Error When uploading with Bootstrap fileinput

0

Well I'm having problems with this plugin I would like to know if someone has already messed with it and if it could help in the question

I'm having problems communicating with the controller I'm taking the following error 405 Method Not Allowed, but just at the time of use of this plugin the other functions of my system as registration query are ok

My controller that receives the request follows

@RequestMapping(value = "/carregarupload", method = { RequestMethod.POST })
public @ResponseBody Object upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
    System.out.println("upload() called");

    if (file.isEmpty()) {
        request.setAttribute("message", "Please select a file to upload");
        return "uploadStatus";
    }

    try {
        byte[] bytes = file.getBytes();
        Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
        Files.write(path, bytes);

        request.setAttribute("message", "You have successfully uploaded '" + file.getOriginalFilename() + "'");

    } catch (IOException e) {
        e.printStackTrace();
    }

    return getSuccessMessage().toString();
}

private JSONObject getSuccessMessage() {
    JSONObject jsonObject = null;
    try {
        jsonObject = new JSONObject("{\"success\":true}");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return jsonObject;
}

and also follows the page requesting the post along with the plugin script

<div class="containerK">
    <form enctype="multipart/form-data" method="${methodName}"
        action="${action}">
        <div class="form-group">
            <input id="file-1" name="file" type="file" multiple class="file"
                data-overwrite-initial="false" data-min-file-count="1">
        </div>
    </form>
</div>



<script>
    $(document).ready(function() {
        $("#file-1").fileinput({
            uploadUrl : '/upload', // you must set a valid URL here else you will get an error
            allowedFileExtensions : [ 'jpg', 'png', 'gif', 'war' ],
            overwriteInitial : false,
            maxFileSize : 10000,
            maxFilesNum : 10,
            //allowedFileTypes: ['image', 'video', 'flash'],
            slugCallback : function(filename) {
                return filename.replace('(', '_').replace(']', '_');
            }
        });
    });

If anyone can help, I'm grateful.

this and upload method driver

@RequestMapping(value = REDIRECT_PAGE_UPLOAD, method = RequestMethod.GET)
 public ModelAndView showUpload(ModelMap model, HttpServletRequest request) {

    model.addAttribute(ControllerConstants.METHOD_NAME, RequestMethod.POST.name());

    model.addAttribute(MODEL_NAME, new ItoBean(false));

    model.addAttribute(HABILITAR_CAMPOS, true);

    model.addAttribute(ControllerConstants.METHOD_NAME, RequestMethod.POST.name());

    String action = String.join(SEPARATOR, request.getContextPath(), ACTION_UPLOAD);

    model.addAttribute(ControllerConstants.ACTION, action);


    return new ModelAndView(REQUEST_MAPPING_PAGE_UPLOAD);
}
    
asked by anonymous 01.06.2017 / 16:08

1 answer

1

Error 405 indicates that the service (POST) does not support the method being invoked. On your form you have used a variable ($ {methodName}) for the method type, it is very likely that it is not being populated, and the action may have the same problem. Try modifying your form like this:

    <body>
    <div class="containerK">
        <form enctype="multipart/form-data" method="POST" action="/carregarupload">
            <div class="form-group">
                <input id="file-1" name="file" type="file" multiple data-min-file-count="0" />
            </div>
        </form>
    </div>

    <script type="text/javascript">
      $(document).ready(function() {
        $("#file-1").fileinput();
      });
    </script>
</body>

@Controller
@RequestMapping("/")
public class UploadController {

  @RequestMapping(value = "/carregarupload", method = {RequestMethod.POST})
  public @ResponseBody String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
    try {
      byte[] bytes = file.getBytes();
      Path path = Paths.get("/home/guilherme/" + file.getOriginalFilename());
      Files.write(path, bytes);
      request.setAttribute("message", "You have successfully uploaded '" + file.getOriginalFilename() + "'");
    } catch (IOException e) {
      e.printStackTrace();
    }
    return "{\"success\":true}";
  }

}
    
01.06.2017 / 17:37