节点js中的ZIP格式的响应头数据 [英] response header data as zip in node js

查看:15
本文介绍了节点js中的ZIP格式的响应头数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了此代码来发送报头中的响应Zip,但我这边缺少一些东西。在此,我得到了如屏幕截图所示的响应

以下是我的代码..

const zipPath - './test.zip'; // I have a zip with 2 files inside it (password protected)
    const stat = fs.statSync(zipPath);
    res.setHeader('Content-Disposition', 'attachment; filename=devices.zip');
    res.setHeader('Content-Type', 'application/octet-stream');
    res.setHeader('Content-Length', stat.size);
     res.writeHead(200, {
       'Content-Type': 'application/octet-stream',
       'Content-Disposition': 'attachment; filename=test.zip',
       'Content-Length': stat.size
     });

    let fileStream = fs.createReadStream(zipPath);
    fileStream.pipe(res);
    res.end();

推荐答案

您似乎可以发送数据,但编码不正确。在您的情况下,内容类型应该是‘应用程序/压缩’,而不是‘应用程序/八位字节-流’。

    const zipPath - './test.zip'; // I have a zip with 2 files inside it (password protected)
    const stat = fs.statSync(zipPath);
    res.setHeader('Content-Disposition', 'attachment; filename=devices.zip');
    res.setHeader('Content-Type', 'application/zip');
    res.setHeader('Content-Length', stat.size);
     res.writeHead(200, {
       'Content-Type': 'application/zip',
       'Content-Disposition': 'attachment; filename=test.zip',
       'Content-Length': stat.size
     });

    let fileStream = fs.createReadStream(zipPath);
    fileStream.pipe(res);
    res.end();

这篇关于节点js中的ZIP格式的响应头数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆