我試圖將使用jsPDF在前端JavaScript上生成的PDF傳遞給Spring Framework MVC後端。下面是前端代碼,我已經寫了:使用二進制數據使用AJAX從jsPDF上傳PDF
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(「文件名」)時,正確打開。以下是已經寫在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:所需的字節[]參數' fileBytes '不存在
我可以在大量的數據的正被髮送的請求有效載荷確認,但是後端似乎不希望接受參數。
這樣做的目的是,我希望能夠從pdf獲取數據,然後在後端對其進行解碼,以便稍後將pdf發佈到服務器上的某個位置。我的代碼中是否存在缺少這些請求的問題,並且是否有更簡單更有效的方法來實現此功能?