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

    一直以来,JS都没有比较好的可以直接处理二进制的方法。而Blob的存在,允许我们可以通过JS直接操作二进制数据。
    一、下载

    util.fetchDownload= function (opt,data) {
    return fetch(opt.url,{
    method: "POST",
    headers: {
    'Content-Type': 'application/json',
    },
    mode: "cors",
    body: JSON.stringify(data),
    credentials: 'include'
    }).then(resp => resp.blob().then(blob => {
    const disposition = resp.headers.get("content-disposition");
    const match = disposition ? disposition.match(/attachment; filename="(.+)"/i) : null;
    const filename = match && match.length > 1 ? match[1] : snTime + ".xls";
    if (window.navigator.msSaveOrOpenBlob) {
    navigator.msSaveBlob(blob, filename); //兼容ie10
    } else {
    var a = document.createElement('a');
    document.body.appendChild(a) //兼容火狐,将a标签添加到body当中
    var url = window.URL.createObjectURL(blob); // 获取 blob 本地文件连接 (blob 为纯二进制对象,不能够直接保存到磁盘上)
    a.href = url;
    a.download = filename;
    a.target='_blank' // a标签增加target属性
    a.click();
    a.remove() //移除a标签
    window.URL.revokeObjectURL(url);
    }
    }).then(function () {
    opt.success()
    }))

    };


  • 相关阅读:
    [bzoj 2460]线性基+贪心+证明过程
    [Wc2011] Xor
    [BZOJ2844]线性基+xor本质不同第K大
    洛谷3857 [TJOI2008]彩灯
    HDU3949 异或线性基
    hdu3062 party --2-sat
    KM算法详解+模板
    Hopcroft-Karp算法
    bzoj 1135: [POI2009]Lyz
    hall定理的证明
  • 原文地址:https://www.cnblogs.com/0828-li/p/10334455.html
Copyright © 2011-2022 走看看