Loading PDF from jsPDF using AJAX using binary data

I am trying to transfer the PDF that I generated on the frontend javascript using jsPDF for Spring Framework MVC firewall. The following is the front end code I wrote:

var filename = "thefile"; var constructURL = '/daas-rest-services/dashboard/pdfPrintUpload/' + filename; var url = restService.getUrl(constructURL); var fileBytes = btoa(pdf.output()); $http.post(url, fileBytes).success(function(data) { console.log(data); }) .error(function(e, a) { console.log(e); console.log(a); }); 

The pdf variable was generated correctly and can confirm that it opens correctly when pdf.save ("filename") is called. The following is the Java code that was written to the Spring MVC firewall for this call:

 @RequestMapping(method = RequestMethod.POST, value = "/pdfPrintUpload/{documentName}") public @ResponseBody String postPrintDocument(@PathVariable String documentName, @RequestParam byte[] fileBytes) { String methodName = "postPrintDocument"; if(logger.isLoggable(Level.FINER)){ logger.entering(CLASS_NAME, methodName); } String check; if(fileBytes != null){ check = "not null"; } else { check = "null "; } //Decoding the bytestream //Save to file location //return file location String returnValue = "HI " + documentName + " " + check; if (logger.isLoggable(Level.FINER)) { logger.exiting(CLASS_NAME, methodName); } return returnValue; } 

Every time I make a request, I get 400 Errors telling me:

Error 400: Required byte [] parameter 'fileBytes' missing

I can confirm in the request payload that a large amount of data is being transferred, however the backend does not seem to want to accept the parameter.

The purpose of this is that I want to be able to receive data from pdf, and then decode it to the backend, in order to subsequently publish the PDF file in place on the server. Is there something that I am missing in my code so that these requests do not work, and is there a more efficient way to achieve this functionality?

+7
java javascript jquery spring ajax
source share
2 answers

The solution changed @RequestParam to @RequestBody. @RequestParam is a parameter that is sent along the path. @RequestParam vs @PathVariable

+4
source share

Try using ng-file-upload. Link and examples are available here.

ng-file-upload

for side code on the side try using this

  @RequestMapping(value = "/pdfPrintUpload") @ResponseBody public void postPrintDocument(@RequestParam("file") MultipartFile file) { InputStream is = file.getInputStream(); OutputStream os = new FileOutputStream(/*path to save file*/); byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) > 0) os.write(buffer, 0, length); is.close(); os.close(); } catch (Exception e) { e.printStackTrace(); } } 
+3
source share

All Articles