当先锋百科网

首页 1 2 3 4 5 6 7

I am getting the below errror when trying to upload multiple files to my ajax request. Below is the error in the logs and my scripts. It looks like its failing on the ajax request as the console.logs are printing out as expected. Im not sure what this means?

HTTP Status 400 - Required MultipartFile[] parameter 'files' is not present

AJAX REQUEST:

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

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[j],

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);

}

});

}

Server side:

private static String UPLOADED_FOLDER = "C://temp//";

@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";

}

}

Configuration

@Bean(name = "multipartResolver")

public CommonsMultipartResolver commonsMultipartResolver(){

CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();

commonsMultipartResolver.setDefaultEncoding("utf-8");

commonsMultipartResolver.setMaxUploadSize(5000000); // 5000000 -> 5MB

return commonsMultipartResolver;

}

}