您的当前位置:首页正文

el-upload上传文件

2024-11-30 来源:个人技术集锦
<el-upload
  :disabled="addType===false"
  :action="url + '?p=' + this.ruleFormCode"
  class="upload-demo"
  :limit="1"
  list-type="picture"
  :file-list="fileList"
  :on-success="handleUpdate"
  :before-upload="handleBefore"
  :headers="headers">
  <el-button size="small" type="primary">点击上传</el-button>
  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>

一般请求的话,需要加token或者是openid等。如果是用el-upload中的action属性,就没有添加token或者openid。所以需要加一个headers中添加。
也可以使用:http-request属性

<el-upload action="#" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload">
  <el-button size="small">
    选择
    <i class="el-icon-upload el-icon--right"></i>
  </el-button>
</el-upload>
requestUpload(item) {
  var formData = new FormData();
  var that = this;
  formData.append("file", item.file);
  uploadFiles(formData).then(response => {
    that.form.attachment += response.msg+",";
    console.log("上传成功!")
  })
}

uploadFiles是封装的请求。比如直接用这个的话。就相当于是之前封装过的request请求,里面已经加了token了,就不需要再添加

export function uploadFiles(data) {
  return request({
    url: '/system/device/inform/uploadFiles',
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    data: data
  })
}
显示全文