SpringMVC+vue实现多图片上传
时间: 2024-01-09 13:03:42 浏览: 106
好的,下面是一个使用SpringMVC和Vue.js实现多图片上传的示例代码:
前端页面代码(使用Vue.js):
```html
<template>
<div>
<input type="file" ref="fileInput" @change="handleFileUpload" multiple>
<button @click="uploadFiles">上传文件</button>
</div>
</template>
<script>
export default {
data() {
return {
files: []
}
},
methods: {
handleFileUpload() {
this.files = this.$refs.fileInput.files;
},
uploadFiles() {
let formData = new FormData();
for (let i = 0; i < this.files.length; i++) {
formData.append("files", this.files[i]);
}
axios.post("/upload", formData, {
headers: {
"Content-Type": "multipart/form-data"
}
}).then(response => {
console.log(response.data);
}).catch(error => {
console.log(error);
});
}
}
}
</script>
```
后端代码(使用SpringMVC):
```java
@PostMapping("/upload")
@ResponseBody
public List<String> upload(@RequestParam("files") MultipartFile[] files) throws IOException {
List<String> fileNames = new ArrayList<>();
for (MultipartFile file : files) {
if (!file.isEmpty()) {
String fileName = file.getOriginalFilename();
String uuid = UUID.randomUUID().toString();
String saveFileName = uuid + "_" + fileName;
file.transferTo(new File("/path/to/save/" + saveFileName));
fileNames.add(saveFileName);
}
}
return fileNames;
}
```
这个示例代码使用了Vue.js来实现前端页面的交互,使用了axios库来发送文件上传请求。后端使用了SpringMVC的MultipartFile来处理文件上传请求。在处理每个文件时,可以根据需要对文件进行一些额外的处理,例如:获取文件大小、文件类型等。
希望这个示例代码可以帮助到您!
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)