文件上传和下载 上传实现流程1、 el-upload 123456789101112131415<el-uploadref="upload":limit="1"accept=".jpg, .png":action="upload.url":headers="upload.headers":file-list="upload.fileList":on-progress="handleFileUploadProgress":on-success="handleFileSuccess":auto-upload="false"><el-button slot="trigger" size="small" type="primary">选取文件</el-button><el-button style="margin-left: 10px;" size="small" type="success" :loading="upload.isUploading" @click="submitUpload">上传到服务器</el-button><div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div></el-upload> 2、data 中添加属性 1234567891011// 上传参数upload: { // 是否禁用上传 isUploading: false, // 设置上传的请求头部 headers: { Authorization: "Bearer " + token }, // 上传的地址 url: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的文件列表 fileList: []}, 3、添加对应事件 1234567891011121314// 文件提交处理submitUpload() { this.$refs.upload.submit();},// 文件上传中处理handleFileUploadProgress(event, file, fileList) { this.upload.isUploading = true;},// 文件上传成功处理handleFileSuccess(response, file, fileList) { this.upload.isUploading = false; this.form.filePath = response.url; this.msgSuccess(response.msg);} 下载实现流程1、添加对应按钮和事件 123456<el-button size="mini" type="text" icon="el-icon-edit" @click="handleDownload(scope.row)">下载</el-button> 2、实现文件下载 1234567891011// 文件下载处理 handleDownload(row) { var name = row.fileName; var url = row.filePath; var suffix = url.substring(url.lastIndexOf("."), url.length); const a = document.createElement('a') a.setAttribute('download', name + suffix) a.setAttribute('target', '_blank') a.setAttribute('href', url) a.click() } vue基础 文件上传和下载 http://example.com/2023/02/23/文件上传和下载/ 作者 dinghw 发布于 2023年2月23日 许可协议 TypeScript 上一篇 数组操作方法 下一篇