vue中文件上传

导入中成药
uploadChineseMedicine() {
     $("#excelUp").click();
},
drugUpload(e,drugType) {
      var file = e.target.files[0];
      let fd = new FormData();
      fd.append("fileName", file);
      fd.append("drugType", drugType);
      this.uploadFile(fd);//导入数据
      this.$refs.fileUpload.value = null;
},
async uploadFile(fd){
      this.loading = true;
      const response = await uploadDrug(this, fd);
      if(response.code == 0){
         this.loading = false;
         this.$message({
             message: response.msg,
             type: "success",
             duration: 1200
          });
      }
},
//根据接口返回数据类型选择这两种方式的一种
async uploadFile(fd) {
                const response = await uploadFile(this, fd);
                let blob = new Blob([response.data], {
                    type: 'application/vnd.ms-excel'
                })
                this.init();
                if (window.navigator.msSaveOrOpenBlob) {
                    // navigator.msSaveBlob(blob, fileName)
                    navigator.msSaveBlob(blob)
                } else {
                    var link = document.createElement('a')
                    link.href = window.URL.createObjectURL(blob)
                    // link.download = fileName
                    link.click()
                    window.URL.revokeObjectURL(link.href) //释放内存
                }
            },

 

你可能感兴趣的:(vue中文件上传)