文件导入导出

导出Excel文件,responseType设置了blob,没下载成功时,后台实际返回了JSON格式的错误信息

downloadInfo(this.form).then(res=>{
        if (res.type == 'application/json') {
    // json信息展示
          const fileReader = new FileReader();
          fileReader.onload = (ev) => {
            try {
              const jsonData = JSON.parse(ev.target.result); 
              this.$message.error(jsonData.data.errMsg)
            } catch (err) { 
              console.log(err)
            }
          }
          fileReader.readAsText(res);
        } else {
          //blob转换成Excel文件   
          downloadData(res, '下载文件名.xlsx')
        }
      }).catch(err=>{
        this.$message.error('出错了,请稍后再试!')
      })
 
 // 下载文件
export function downloadData(data, name) {
  const url = window.URL.createObjectURL(new Blob([data]))
  if (window.navigator.msSaveBlob) {
    try {
      window.navigator.msSaveBlob(new Blob([data]), name)
    } catch (e) {
      console.log(e);
    }
  } else {
    const link = document.createElement('a')
    link.style.display = 'none'
    link.href = url
    link.setAttribute('download', name)
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
  }

elementUI实现文件立即上传

        <el-upload
          ref="upload"
          :show-file-list="false"
          :http-request="upload"
          :auto-upload= "true"
          action=""
          size="small"
          multiple>
          <el-button  type="primary" size="mini" icon="el-icon-upload2">导入</el-button>
        </el-upload>
    upload(param) {
      const formData = new FormData()
      formData.append('file', param.file)
      uploadInfo(formData).then(response => {
        if (response.status === "1") {
          this.$message.success('文件上传成功!')
        } else {
          this.$message.error(response.data.errMess)
        }
      }).catch(error => {
        this.$message.error('出错了,请稍后再试!')
      })
      this.$refs.upload.clearFiles()
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值