有誰知道我爲什麼會出現上述錯誤?我不明白爲什麼?請參閱下面的代碼,並告知我可以修復此錯誤的位置。目的是將多個文件上傳到一個位置。它用於單個文件,但它看起來像formdata或ajax請求只用於接受一個文件而不是多個。我不是在PHP中這樣做,只有javascript/java。請幫忙。post error需要MultipartFile []參數不存在
function makeProgress(number){
var url = getRelativeURL("web/fileUpload");
var formData = new FormData();
formData.append('number', number);
fls = document.getElementById("attachmentFileUploadInput").files; //length of files...
console.log(fls);
for(j=0;j<fls.length;j++){
formData.append('files[]', fls[j]); //note files[] not files
}
//formData.append('file', $('input[type=file]')[0].files[0]);
console.log("form data " + formData);
$.ajax({
url : url,
data : formData,
processData : false,
contentType : false,
type : 'POST',
success : function(data) {
FileUploadVisible(true);
$('#attachmentModal').modal('hide')
$(':input','#attachmentModal').val("");
$("#pbarmain").hide();
$("#pbar").hide();
$("#actionPlanDiv").hide();
setObjectEnabled('#Upload',false);
},
error : function(err) {
FileUploadErrorVisible(true);
}
});
}
@Bean(name = "multipartResolver")
public CommonsMultipartResolver commonsMultipartResolver(){
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
commonsMultipartResolver.setDefaultEncoding("utf-8");
commonsMultipartResolver.setMaxUploadSize(5000000); // 5000000 -> 5MB
return commonsMultipartResolver;
}}
@RequestMapping(value = { "/fileUpload" }, method = RequestMethod.POST)
@ResponseBody
public String uploadFile(@RequestParam("number") String number, @RequestParam("files") MultipartFile[] files, MultipartHttpServletRequest req, HttpServletResponse res)
{
for (MultipartFile file : files) {
try {
File directory = new File(UPLOADED_FOLDER + number);
if (! directory.exists()){
directory.mkdir();
}
byte[] bytes = file.getBytes();
Path path = Paths.get(UPLOADED_FOLDER + number + "//" + file.getOriginalFilename());
Files.write(path, bytes);
logger.info("You have successfully uploaded '" + file.getOriginalFilename() + "'");
return("File Uploaded");
} catch (Exception e) {
res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
logger.error("Failed to upload file '" + file.getOriginalFilename() + "'", e);
return("File Not Uploaded");
}
}
return "redirect:/fileUpload";
}
}
我不熟悉AJ中的文件上傳AX或Java HTTP集成,但只是看看錯誤,它表明你應該有一個MultipartFile數組作爲參數somwhere,可能來自CommonsMultipartResolver?錯誤發生在哪裏? – dodo
錯誤來自Spring日誌,當我在本地使用Tomcat服務器運行它時,當我在本地執行我的代碼時,我可以選擇多個文件,然後當我點擊上傳按鈕時,它打印控制檯日誌: console .log(「form data」+ formData); 和ajax請求後的錯誤。 – RA19