使用二进制数据使用AJAX从jsPDF上传PDF

Joe*_*Urc 7 javascript java ajax jquery spring

我试图将使用jsPDF生成的前端javascript生成的PDF传递给Spring Framework MVC后端.以下是我写的前端代码:

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

  });
Run Code Online (Sandbox Code Playgroud)

pdf变量已正确生成,并且在调用pdf.save("filename")时可以确认打开.下面是为此调用编写的Spring MVC后端的Java代码:

@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;
}
Run Code Online (Sandbox Code Playgroud)

每次我提出请求时,我都会收到400错误告诉我:

错误400:必需的byte []参数'fileBytes'不存在

我可以在请求有效负载中确认正在传输大量数据,但后端似乎不想接受该参数.

这样做的目的是我希望能够从pdf获取数据,然后在后端对其进行解码,以便稍后将pdf发布到服务器上的某个位置.我的代码中是否存在一些缺少这些请求以保持失败的内容,是否有更简单有效的方法来实现此功能?

Joe*_*Urc 4

解决方案是将@RequestParam更改为@RequestBody。@RequestParam是在路径中发送的参数。 @RequestParam 与 @PathVariable