常见文件下载方法

本文介绍了使用Node.js中的文件流进行文件下载的方法,并通过Pipe()实现数据传输。此外还探讨了利用Blob对象进行文件下载的技术,包括创建Blob对象、设置文件类型及触发下载过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值