我正在尝试将多个文件上传到一个文件夹,因为我在日志中收到错误,所以这很有效:"必需的MultipartFile参数' file'不存在"。请看下面的代码并指导我出错的地方。
<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");
}
}
}
答案 0 :(得分:0)
contentType: 'multipart/form-data'
这就是你所缺少的