0
我想上傳多個文件到一個文件夾,這是工作,因爲我在日誌中收到錯誤:「所需的MultipartFile參數'文件'不存在」。請參閱我的下面的代碼,並指示我哪裏出錯。多個Ajax表單數據上傳jquery
<input type="file" id = 'attachmentFileUploadInput' multiple style="display: none;" >
function makeProgress(number){
var url = getRelativeURL("web/fileUpload");
var formData = new FormData();
formData.append('number', number);
fls = document.getElementById("attachmentFileUploadInput").files; //length of files...
for(j=0;j<fls.length;j++){
formData.append('files[]', fls[j]); //note files[] not files
}
// this wroked for single file upload -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);
}
});
}
服務器端:
private static String UPLOADED_FOLDER = "C://temp//";
@RequestMapping(value = { "/fileUpload" }, method = RequestMethod.POST)
@ResponseBody
public String uploadFile(@RequestParam("number") String number, @RequestParam("file") MultipartFile file, MultipartHttpServletRequest req, HttpServletResponse res)
{
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");
}
}
}
你用'文件附加文件名[]',但在服務器端的期望與名'file'如:' @RequestParam(「文件」)MultipartFile文件' –
如何更新服務器端以期望文件[],我需要更新我的代碼? – RA19
您是否找到您的解決方案? –