常见文件下载方法

  1. node 文件流下载

.pipe()

            request(Base.mergeRequestOptions({
                    http: DWY_GLOBAL.server.Static.http,
                    host: DWY_GLOBAL.server.Static.host,
                    port: DWY_GLOBAL.server.Static.port,
                    headers:{'Content-type':'application/json'},
                    method: 'post',
                    url: '/zipDownload',
                    body:JSON.stringify(data),
                }, req, res)).pipe(res)
                
              request(Base.mergeRequestOptions({
                    method: 'get',
                    url: '/api/orders/completeSet/export?'+ queryString.stringify(req.query),
                }, req, res)).pipe(res)

2.Blob

$http.post(URL , {
    parameter:parameter
},{
    responseparameterType: 'arraybuffer'
}).then(function (res) {
    var blob = new Blob([res.data], {type: 'application/vnd.ms-excel'}),fileName = '文件名称';
    downFile(blob, fileName);
)};

function downFile(blob, fileName) {
    if (window.navigator.msSaveOrOpenBlob) {
        navigator.msSaveBlob(blob, fileName);
    } else {
        var link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.download = fileName;
        link.click();
        window.URL.revokeObjectURL(link.href);
    }
}

3.模拟a标签

  const saveLink = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
  saveLink.href = 'http://jhtcc.com/Img/5a6e9b150aa98.png';
  saveLink.download = '5a6e9b150aa98.png';
  const event = document.createEvent('MouseEvents');
  event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  saveLink.dispatchEvent(event);

你可能感兴趣的:(download)