zoukankan      html  css  js  c++  java
  • 文件下载POST

    好久没有写东西了,因为我换了一家公司但是,居然比之前还忙。之前有写过get的文件自动下载,今天记录一下post自动下载。

    export async function downloadBlob() {
      let res = await consts.axios.post(saveUrl, params, {
        responseType: 'arraybuffer',
      });
    
      if (!res) return;
      try {
        //如果不报错,说明后台返回的是json对象,则弹框提示
        //如果报错,说明返回的是文件流,进入catch,下载文件
        let enc = new TextDecoder('utf-8');
        res = JSON.parse(enc.decode(new Uint8Array(res.data))); //转化成json对象
        if (res.code == '200') {
            //操作成功,
          });
        } else {
          //接口请求失败
        }
      } catch (err) {
        const name = res.headers['content-disposition'].match(/filename="(S*)"|filename=(S*)/)
        const fileName = decodeURIComponent(name[1] || name[2]);
        downloadAttachment(res.data, fileName);
      }
    }
    
    export function downloadAttachment(data, fileName) {
      let fileContent = data;
      let a = document.createElement('a');
      a.download = fileName;
      a.href = global.URL.createObjectURL(new Blob([fileContent]));
      a.click();
    }
    

      

  • 相关阅读:
    敏捷开发感想
    团队分工
    My Partner‘s Code View
    课堂上面的练习
    APP测试用例
    Android App测试计划和设计测试矩阵
    BugReport-智慧农业APP
    图书管理系统的活动图和时序图
    图书管理系统用例图
    对图书管理系统5W1H的分析
  • 原文地址:https://www.cnblogs.com/Ewarm/p/15352669.html
Copyright © 2011-2022 走看看