Node.js 无法将 x-zip-compressed 转换为 application/zip

标签 node.js zip azure-blob-storage mime-types npm-request

这是我的代码的一部分,它获取上传到 Azure blob 的 zip 文件,并将其发送回调用者。

Azure 返回的内容类型是 application/x-zip-compressed。但是,我需要返回application/zip。如果我没有在代码中设置 header ,它将返回 text/plain;字符集=utf-8。如果我按照下面的代码设置 header ,它将返回 application/zip;字符集=utf-8。结果是即使文件被下载,我也无法用 winzip 打开它。

我的问题是,如何将文件转换为 application/zip

var express = require('express');
var request = require('request');
var router = express.Router();

router.post('/', function (req, res) {
request.get("https://path-to-my-azure-storage-blob/"+ fileName, function (error, response, body) {
  if (error){
    console.log('error:', error); // Print the error if one occurred
  } else {
      console.log("Status Code: ", response.statusCode)
      console.log("Content-type: ", response.headers['content-type'])
      res = {
      "status": 200,
      "headers": {
        'Content-Type': 'application/zip'
       },
      "body": body
    };
    res.end();
  }
});

})

最佳答案

为什么下载的 blob/zip 被“损坏”的答案在这里 https://stackoverflow.com/a/12029764/1225266

所以解决方案是(如果您愿意,可以进一步简化)

request.get("https:/....blob.core.windows.net/folder/some.zip", null)
    .on('error', (error) => {
        console.log(error);
    })
    .on('response', function(response) {
        res.writeHead(200, {
            'Content-Type': response.headers['content-type'],
            'Content-disposition': 'attachment;filename=anotherfilename.zip',
            'Content-Length': response.headers['content-length']
        }); 
      })
    .on('data', (d) => {
        res.write(d);
    })
    .on('end', () => {
        res.end()
    });

您可以在此处了解如何将响应从 azure 直接传送到客户端 https://github.com/request/request#streaming

关于Node.js 无法将 x-zip-compressed 转换为 application/zip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52936430/

相关文章:

javascript - 附加base64编码文件nodejs

c# - 以 zip 格式下载文件会导致 ASP.NET MVC 中的压缩文件损坏

angular - 413 IIS 请求实体太大

linux - 如何使用nodejs创建一个linux用户

Javascript:错误:找不到模块 '../data/icons/table.png'

javascript - 在 selenium webdriver javascript 绑定(bind)中创建 "custom"Promise

javascript - 使用 Wea​​syprint 生成 pdf 文件,保存为 zip 文件,将该 zip 文件发送给客户端并提供下载

java - ZIP 目录 AES 加密

azure - 您可以将流量管理器与 blob 存储或 Azure 功能结合使用吗

C# 从 azure 存储容器路径解压缩文件