Error While Uploading with Bootstrap fileinput

Asked

Viewed 262 times

0

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

I am having problems communicating with the controller I am taking the following error 405 Method Not Allowed, but only when using this plugin the other functions of my system as registration query are ok

follow my controller who receives the request

@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 that makes the post request 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 controller

@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);
}

1 answer

1

Error 405 indicates that the service (POST) does not support the method being invoked. In your form you used a variable (${methodName}) for the type of method, it is quite likely that it is not being filled in, and the action may be with 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}";
  }

}
  • opa then guy tried to leave the way you indicated put the error continues

  • @Wesleysantos Error (405 Method Not Allowed) continues? I tested your code not using jQuery’s fileInput but with a Ubmit button in the form and it worked normally, another question: How is mapped the Requestmapping of the Controller where the method is upload

  • then the error persists I will edit and put the rest of my controller so you understand the whole context

  • so if you could send me the example you created on top of my code so I can test

  • @Wesleysantos with these variables in his code has no way I understand how the contexts are being mounted, try to validate if the path and the methods are hitting, because its implementation is correct

  • guy I tried to implement

  • but still I keep making the error 405

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.