【问题标题】:Send zip file as response in IBM Cloud Function在 IBM Cloud Function 中发送 zip 文件作为响应
【发布时间】:2019-10-07 19:13:42
【问题描述】:

我在 IBM Cloud Functions 中部署了一个无服务器函数。该函数生成并下载一个excel文件。

在本地它按预期工作,但在部署的版本上我收到以下错误:

{
  "code": "xxxxxxxxxx",
  "error": "Response type in header did not match generated content type."
}

使用 Content-Type 标头 'application/json' 我可以发送 json 和 'image/png' 甚至图像作为 base64。 Zip 文件不起作用。我试过'Content-Type': 'application/zip'Content-Type': 'application/zip, application/octet-stream

我需要做更多的配置吗?

完整示例:

headers: {
    'Content-Type': 'application/zip',
    'Content-Disposition': `attachment; filename=foo.zip;`,
    'Content-Transfer-Encoding': 'binary',
    'Content-Length': zip.length
},

【问题讨论】:

    标签: node.js zip ibm-cloud mime-types serverless-framework


    【解决方案1】:

    我通过生成 base64 zip 而不是 uint8array 来修复它。

    之前:

    var JSZip = require("jszip");
    var zip = new JSZip();
    
    const myZip = await zip.generateAsync({
                    type: 'uint8array',
                    base64: true,
                    compression: 'DEFLATE'
                });
    

    之后:

    var JSZip = require("jszip");
    var zip = new JSZip();
    
    const myZip = await zip.generateAsync({type: 'base64'});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      • 2012-04-20
      • 2019-05-14
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多