【问题标题】:Node.js Not able to convert x-zip-compressed to application/zipNode.js 无法将 x-zip-compressed 转换为 application/zip
【发布时间】:2019-03-26 22:43:21
【问题描述】:

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

Azure 返回的内容类型是application/x-zip-compressed。但是,我需要返回application/zip。如果我没有在我的代码中设置标题,它会返回text/plain; charset=utf-8。如果我在下面的代码中设置标题,它将返回application/zip; charset=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();
  }
});

})

【问题讨论】:

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


    【解决方案1】:

    为什么下载的 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()
        });
    

    你可以在这里https://github.com/request/request#streaminghttps://github.com/request/request#streaming了解如何将来自 azure 的响应直接传送到客户端

    【讨论】:

      猜你喜欢
      • 2020-06-15
      • 1970-01-01
      • 2011-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多