I have a web form that I'm trying to add multi-file upload to. Currently, I can select a folder and upload multiple files. I have a Spring controller which gets the List<MultipartFile>
containing all the files, resucively. However, the "original file name" includes JUST the file name. What I want is the relative path from the selected root folder, and the file name.
For example, if user uploads a directory C:\MyStuff\mypics\, I'd want to see "dog\dog1.jpg", "cat\cat5.jpg", etc. Or, "mypics\dog\dog1.jpg" would be acceptable.
HTML:
<button ngf-select="myController.uploadTest($files)"
multiple="multiple" webkitdirectory accept="image/*">Select Files ngf</button>
AngularjS Controller:
// for multiple files:
myController.uploadFiles = function (files) {
console.log("uploading files: " + files.length);
if (files && files.length) {
// send them all together for HTML5 browsers:
Upload.upload({
url: 'load-service/upload-test',
data: {file: files, myVal1: 'aaaa'},
// setting arraykey here force the data to be sent as the same key
// and resolved as a List<> in Spring Controller.
// http://stackoverflow.com/questions/35160483/empty-listmultipartfile-when-trying-to-upload-many-files-in-spring-with-ng-fil
arrayKey: ''
}).then(function (response) {
// log
}, function (response) {
// log
}, function (event) {
// log that file loaded
});
}
}
Java Spring Controller:
@PostMapping(value="upload-test")
public ResponseEntity<?> uploadTest(@RequestBody List<MultipartFile> file) {
try {
LOGGER.info("Received file set of size: " + file.size());
for (int i = 0; i < file.size(); i++) {
// testing, should be debug
MultipartFile singleFile = file.get(i);
String fileName =singleFile.getName();
String originalFileName = singleFile.getOriginalFilename();
LOGGER.info("Handling file: " + fileName);
LOGGER.info("Handling file (original): " + originalFileName);
LOGGER.info("File size: " + singleFile.getSize());
LOGGER.info("--");
}
return ResponseEntity.ok().body(new GeneralResponse("Handled file list of size: " + file.size()));
} catch (Exception ex) {
String msg = "Error getting files";
LOGGER.error(msg, ex);
return ResponseEntity.badRequest().body(new GeneralResponse(msg, ex));
}
}
I see that my controller is being called, but there's nothing I can see in teh MultipartFile objects that tell me the relative path of the files. When I debug in my browser, I can see that the files, prior to upload, have a field of webkitRelativePath attribute which has the relative path, but I don't see how to transfer that over to the server side in Spring.
Do I need to upload one file at a time and provide the relative path for each file as an optional argument to the call?