Vue实现文件上传(参数名称:file;请求格式:formData)

1、后端接口文档:

导入:POST /import [http:-192.168.1.1-%.....xlsx]

2、前端API获取:

//导入
export function eventImport(obj) {
    return request({
        url: '/api/accident/import',
        method: 'post',
        data: obj
    })
}

3、前端页面:

<el-button type="primary" icon="" @click="importFile()" v-if="curFileList.length">确认导入</el-button>
          <el-upload style="display: inline-block;margin-left: 10px" v-if="!curFileList.length" class="upload-demo" ref="upload" action="#" accept=".xls,.xlsx" :on-remove="handleRemove" :file-list="curFileList" :auto-upload="false" :show-file-list="false"
            :on-change="handleChange">
            <el-button type="primary" icon="">导入模板</el-button>
          </el-upload>
          <a href="http:/192.168.1.1.....xlsx" style="color: #fff;margin-left: 10px;font-size: 12px;">下载模板</a>
import { eventAdd, eventDel, eventImport, eventPage, eventUpdate } from '@/api/screen/eventData'; //获取列表数据api

export default {
	components: {},
	data() {
		return {
			curFileList: [],
		};
	},
	methods: {
		/* 导入数据功能 */
		importFile() {
			const self = this;
			if (this.curFileList.length == 0) {
				this.$notify({
					title: '提示',
					message: '请上传Excel文件',
					type: 'warning',
					duration: 2000,
				});
				return;
			}
			let formData = new FormData();
	        this.curFileList.forEach((val, index) => {
                formData.append('file', val.raw);
    	    })
			eventImport(formData).then((res) => {
				self.$notify({
					title: '成功',
					message: '导入成功',
					type: 'success',
					duration: 2000,
				});
				self.curFileList = [];
				self.getDataList();
			});
		},
		handleChange(file, fileList) {
			this.curFileList = [];
			this.curFileList.push(file);
		},
		handleRemove(file) {
			this.curFileList = [];
		},
	},
};

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如何实现SpringBoot+Vue文件上传文件上传涉及前端和后端两个方面的实现前端Vue代码: 1. 定义上传文件的模板: ``` <template> <div> <input type="file" @change="handleFileUpload" ref="fileUpload"> <button @click="submitFile">上传文件</button> </div> </template> ``` 2. 在Vue的methods中添加上传文件的方法: ``` methods: { handleFileUpload () { this.file = this.$refs.fileUpload.files[0] }, submitFile () { let formData = new FormData() formData.append('file', this.file) axios.post('/api/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' } }) .then(response => { console.log(response.data) }) } } ``` 这个方法中,我们通过FormData对象来将文件对象上传到服务器端。需要注意的是,在axios请求中,我们需要指定Content-Type为multipart/form-data,以便后端能够正确地解析上传的文件。 后端的SpringBoot代码: 1. 配置文件上传的Multipart配置 在application.properties文件中添加以下配置: ``` spring.servlet.multipart.max-file-size=10MB spring.servlet.multipart.max-request-size=10MB ``` 这个配置指定了上传文件的大小限制,例如,上限设置为10MB。 2. 添加文件上传的Controller ``` @RestController @RequestMapping("/api") public class FileUploadController { @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file) { try { // 将上传的文件保存到指定路径下 String filePath = "C:/uploads/" + file.getOriginalFilename(); file.transferTo(new File(filePath)); return "文件上传成功"; } catch (IOException e) { e.printStackTrace(); return "文件上传失败"; } } } ``` 这个Controller中,通过@RequestParam注解来指定上传的文件参数名,再通过MultipartFile来获取上传的文件。最后,将文件保存到指定的路径下。需要注意的是,保存路径需要在业务中合理设置。 至此,SpringBoot+Vue文件上传实现就完成了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sun Peng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值