0
我想添加@RequestParam
到我的http
請求,所以它會匹配spring MVC @RequestParam
。Angular - 文件上傳請求,添加@RequestParam
如何添加這對我的文件上傳請求:
/*
@param file - file from input
@param uploadUrl - url
*/
this.uploadFileToUrl = function(file, uploadUrl){
var fd = new FormData();
//add file to FormData
fd.append(file.name, file);
//send request
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function(result){
console.log(result);
})
.error(function(err){
console.log(err);
});
}
在我的後端錯誤是Required String parameter 'filename' is not present
這裏是我的Spring MVC controller
(只有頭部分):
@Controller
@RequestMapping("/file")
public class FileUploadController {
/**
* Upload single file using Spring Controller
*/
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public @ResponseBody String uploadFileHandler(@RequestParam("filename") String filename, @RequestParam("file") MultipartFile file) {
//rest of the function
}
我認爲你已經諮詢了Angular'$ http'或'FormData'的文檔,並試圖按照描述添加參數。你遇到過什麼問題? – zeroflagL
後臺錯誤 - 必需的字符串參數'文件名'不存在,錯誤代碼400 –
沒關係,我想出了自己,發佈了答案。 –