Node js 中的响应标头数据为 zip

2024-04-22

我已经尝试使用此代码在标头中发送响应 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();

看起来您可以发送数据,但编码不正确。在您的情况下,内容类型应该是“application/zip”,而不是“application/octet-stream”。

    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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Node js 中的响应标头数据为 zip 的相关文章

随机推荐