我正在尝试将使用 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);
});
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;
}
每次我发出请求时,我都会收到 400 错误告诉我:
错误 400:所需的 byte[] 参数“fileBytes”不存在
我可以在请求有效负载中确认正在传输大量数据,但是后端似乎不想接受该参数。
这样做的目的是希望能够从 pdf 中获取数据,然后在后端对其进行解码,以便稍后将 pdf 发布到服务器上的某个位置。我的代码中是否遗漏了一些导致这些请求不断失败的内容,是否有更简单、更有效的方法来实现此功能?
最佳答案
解决方案是将@RequestParam 更改为@RequestBody。 @RequestParam 是在路径中发送的参数。 @RequestParam vs @PathVariable
关于javascript - 使用二进制数据通过 AJAX 从 jsPDF 上传 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32341270/