我有一个网页表单,我试图添加多文件上传。目前,我可以选择一个文件夹并上传多个文件。我有一个Spring控制器,它可以获得包含所有文件的List<MultipartFile>
。但是,&#34;原始文件名&#34;包括JUST文件名。我想要的是从选定的根文件夹和文件名的相对路径。
例如,如果用户上传目录C:\ MyStuff \ mypics \,我想要看到&#34; dog \ dog1.jpg&#34;,&#34; cat \ cat5.jpg&# 34;等等或者,&#34; mypics \ dog \ dog1.jpg&#34;是可以接受的。
HTML:
<button ngf-select="myController.uploadTest($files)"
multiple="multiple" webkitdirectory accept="image/*">Select Files ngf</button>
AngularjS控制器:
// 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));
}
}
我看到我的控制器正在被调用,但是我在MultipartFile对象中看不出任何东西告诉我文件的相对路径。当我在浏览器中调试时,我可以看到上传之前的文件有一个webkitRelativePath属性字段,该字段具有相对路径,但我不知道如何将它转移到Spring的服务器端。
我是否需要一次上传一个文件并提供每个文件的相对路径作为呼叫的可选参数?