jquery 获取文件的路径path和名称name将其组装到flilearry里面然后传到java后台Controller
你可以使用<input type="file">元素来选择文件,然后使用JavaScript获取文件的路径和名称,并将它们组装到一个数组中。接下来,你可以将该数组作为参数传递给Java后台的Controller。
HTML代码:
<input id="fileInput" type="file" multiple>
<button id="uploadButton">上传</button>
JavaScript代码:
$('#uploadButton').click(function() {
var fileArray = [];
var files = $('#fileInput')[0].files;
for (var i = 0; i < files.length; i++) {
var file = files[i];
var path = file.name; // 文件路径
var name = file.name; // 文件名称
fileArray.push({ path: path, name: name });
}
// 将fileArray传递给后台Controller
$.ajax({
url: '/your-controller-url',
method: 'POST',
data: JSON.stringify(fileArray),
contentType: 'application/json',
success: function(response) {
// 处理成功的响应
},
error: function(error) {
// 处理错误
}
});
});
在Java的Controller中,你可以使用@RequestBody注解将传递的JSON数据映射到一个Java对象:
@Controller
@RequestMapping("/your-controller-url")
public class YourController {
@PostMapping
public ResponseEntity<?> uploadFiles(@RequestBody List<FileData> fileArray) {
// 处理文件数组
// 返回响应
}
}
public class FileData {
private String path;
private String name;
// Getter和Setter方法省略
}
这样,你就可以在Java后台的Controller中获取文件路径和名称,并对它们进行进一步处理
原文地址: https://www.cveoy.top/t/topic/ioSZ 著作权归作者所有。请勿转载和采集!