首先看一下手动上传带参数的:
这里点击上传按钮不会进行上传,只有点击手动上传按钮的时候才会带参数进行上传,看一下实现代码:
ref="upload" class="upload-demo" action="javascript:void(0);" :limit="1" :show-file-list="false" :auto-upload="false" :on-change="onChange" :disabled="fileList.length > 0 ? true : false" accept=".xlsx,.xls" > size="small" type="primary" :disabled="fileList.length > 0 ? true : false" > 点击上传
{{ item.name }}
export default {
data() {
return {
fileList: [], // 文件列表
id: '11',
}
},
methods: {
// 选中文件
onChange(file) {
this.fileList.push(file)
},
// 移除文件
onRemove() {
this.fileList = []
this.$refs.upload.clearFiles()
},
// 确定
async onSure() {
const formData = new FormData()
//把接口需要的参数带进去
formData.append('id', this.id)
formData.append('file', this.fileList[0].raw)
//点击确定按钮调接口
const res = await inter(formData)
console.log(res)
},
},
}
.upload {
margin: 200px;
}
.lists {
margin-top: 20px;
.list {
width: 200px;
display: flex;
align-items: center;
&:hover {
background-color: #f5f7fa;
}
span {
width: 0;
flex-grow: 1;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.close-i {
cursor: pointer;
}
}
}
.handle-btn{
margin-top: 30px;
}
这里的action="javascript:void(0);"就是告诉浏览器不自动提交,当点击按钮需要提交的时候才会执行上传.
自动上传需要携带参数的:
看一下实现代码:
ref="uploadFile" :action="`${baseUrl}/invoice/uploadZip`" :before-upload="handleBeforeUpload" class="upload-demo" :data="{ pid: form.methods, }" drag :headers="uploadHeader" :on-error="handleError" :on-success="handleUploadSuccess" :show-file-list="false" >
import { baseURL } from '@/config'//需要看你当前项目的地址是写在哪里的,引入即可
import { getToken } from '@/utils/token'//封装的获取token的方法,可以自己定义
export default {
data() {
return {
baseUrl: baseURL, //上传的公共地址
uploadHeader: {
token: getToken(), //上传接口需要携带的token,也可以让后端帮你把接口过滤,就可以不用携带token
},
form: {
methods: '21', //上传接口需要携带的其他参数
},
}
},
methods: {
// 上传之前
handleBeforeUpload(file) {
//图片上传之前的操作,规定图片类型
let fileName = file.name
let pos = fileName.lastIndexOf('.')
let lastName = fileName.substring(pos, fileName.length)
if (lastName.toLowerCase() !== '.zip') {
this.$message.error('文件必须为 .zip 类型')
this.$refs.uploadFile.clearFiles()
return false
}
},
//上传失败
handleError() {
this.$message.error('上传失败,请重试')
},
//图片上传成功
handleUploadSuccess(res) {
console.log(res, '图片上传成功的操作')
},
},
}
.upload-content {
width: 68%;
margin: auto;
margin-top: 50px;
.el-form {
margin: auto;
}
.upload-tips {
font-size: 18px;
font-family: PingFangSC-Medium, PingFang SC;
font-weight: 500;
color: #20354a;
margin-top: 40px;
margin-bottom: 40px;
}
.upload-img {
width: 220px;
height: 48px;
background: #2991ff;
border-radius: 4px;
color: #fff;
line-height: 48px;
margin: auto;
margin-top: 40px;
}
.upload-desc {
font-size: 14px;
font-family: PingFangSC-Medium, PingFang SC;
font-weight: 500;
color: #a6b5c4;
margin-top: 30px;
}
}
::v-deep {
.el-upload {
width: 100%;
}
.el-upload-dragger {
width: 100%;
height: 420px;
}
.el-upload-dragger {
border: 2px dashed #d9d9d9;
}
.el-upload-dragger .el-icon-upload {
font-size: 130px;
}
.el-radio__label {
font-size: 16px;
padding-left: 20px;
}
.el-radio-group {
width: 100%;
text-align: center;
}
}
自动上传action就需要写上上传的地址,data里面就是除上传的文件外其他的参数
自动上传和手动上传带参数就实现了,有需要的可以试试看,完结!
发表评论