上传文件,走action方式(快速)

1、请求头加token

const token = localStorage.getItem('token');
let aothrization = '';
if (token) {
  aothrization = 'Bearer ' + JSON.parse(token);
  this.reqHeader = {
    Authorization: aothrization
  };
}

2、html

<a-upload
          name="file"
          v-model:file-list="fileList"
          @change="fileUploadClickHandle"
          accept=".xlsx,.xls,.csv"
          action="/bi-dataview/api/dataset/excel/upload"
          :showUploadList="false"
          :headers="reqHeader"
        >
          <a-button style="width: 150px">
            <UploadOutlined />
            上传文件
          </a-button>
</a-upload>

3、请求处理

const fileUploadClickHandle = (e: any) =>  {
  setTimeout(() => {
    const { status, response } = e?.file || {};
    if (status == 'uploading' && this.uploadCount == 0) {
      this.loading = startLoading('文件上传中');
      this.uploadCount++;
    }
    if (status == 'done' && response?.success) {
      const { sheets, excelLabel, id } = response?.data;
      if (sheets?.length) {
        const data = sheets[0];
        this.dirSelect('', { node: data });
        this.checkedKeys.push(data.id);
        this.expandedKeys.push(id);
        this.dataSetName = data.datasetName;
        this.selectedKeys = [data.id];
        this.selectSingleData = data;
        this.treeData.push({
          title: excelLabel,
          type: id,
          key: id,
          children: sheets?.map((item: any, index: any) => {
            item.title = item.excelLabel;
            item.type = item.id;
            item.key = item.id;
            item.sonId = item.id;
            return item;
          })
        });
        this.dirCheck('');
      }
      this.loading.close();
      this.uploadCount = 0;
      message.success('上传成功');
    } else if (status == 'done' && !response?.success) {
      this.loading.close();
      this.uploadCount = 0;
      message.error(response?.errorDesc);
    }
  }, 0);
},
posted @ 2024-01-18 15:43  SKa-M  阅读(7)  评论(0编辑  收藏  举报