Search code examples
javascriptjavajqueryspringdropzone

How to pass extra parameter with JQuery Dropzone to my java controller method


I have a dropzone function to upload pdf files and receive it in my controller method to process and save in database, but i need to pass an extra parameter which is the value from a select, i already pass it but i receive "null" value in controller method.

this is mi JQuery dropzone method:

 ...
 $("div#uploadFile").dropzone({ 
                        
     url: "/demo/uploadFile"
     ,params: {
               name: $( "#selectedName" ).val()
              }
    ,acceptedFiles: ".pdf"
    ,clickable: false
    ,maxFilesize: 100
    ,addRemoveLinks: true
    ,init: function() {
                         
              this.on('addedfile', function(file) {
                         //i can see the value in alert
                         alert($( "#selectedName" ).val()); 
              });
              this.on('success', function(file, json) {
                         alert("ok");
              });
               
  });

the controller method (trying to receive in requestParam):

 @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
        public ResponseEntity<?> uploadFile( @RequestParam("name") String name, MultipartFile file)  {

          //here print parameter and is  NULL
          System.out.println("My extra param received is : "+ name);
         
         //process the file-pdf
          ...
          ....

}

Any idea? thanks in advance!


Solution

  • Can you try:

     $("div#uploadFile").dropzone({ 
                            
         url: "/demo/uploadFile"
        ,acceptedFiles: ".pdf"
        ,clickable: false
        ,maxFilesize: 100
        ,addRemoveLinks: true
        ,init: function() {
                             
                  this.on('addedfile', function(file) {
                             //i can see the value in alert
                             alert($( "#selectedName" ).val()); 
                  });
                  this.on("sending", function(file, xhr, formData) {
                            formData.append("name", $( "#selectedName" ).val());
                            console.log(formData);
                  });
                  this.on('success', function(file, json) {
                             alert("ok");
                  });
                   
      });