【问题标题】:How to save .xls data received from http request to file as a in node.js如何将从http请求接收到的.xls数据保存为node.js中的文件
【发布时间】:2019-02-16 17:50:09
【问题描述】:

我想使用从 HTTP 请求接收到的数据创建一个 XLS 文件。 我尝试使用 FS.writeFile() 保存它,但没有运气。

我正在使用 request npm 模块来执行 HTTP 请求:

request(url, function(err, res, data) {
    if(err || res.statusCode !== 200) return;
    fs.writeFileSync('test.xls', data);
});

回复:

 {
          "headers": {
            "content-type": "application/vnd.ms-excel",
            "transfer-encoding": "chunked",
            "connection": "close",
            "content-disposition": "attachment;filename=\"Detail_Tally.xls\"",
            "cache-control": "max-age=0",
            "x-content-type-options": "nosniff"
          },
          "request": {
            "uri": {
              "protocol": "https:",
              "slashes": true,
              "auth": null,
              "host": "testserver.com",
              "port": 443,
              "hostname": "testserver.com",
              "hash": null,
              "search": null,
              "query": null,
              "pathname": "/report/exportExcel",
              "path": "/report/exportExcel",
              "href": "https://testserver.com/report/exportExcel"
            },
            "method": "POST",
            "headers": {
              "content-type": "multipart/form-data; boundary=--------------------------896095632834245509104518",
              "content-length": 1393
            }
          }
        }

【问题讨论】:

    标签: node.js xls fs


    【解决方案1】:

    将编码选项设置为null后它工作

    request(url, {encoding: null}, function(err, res, data) {
        if(err || res.statusCode !== 200) return;
        fs.writeFileSync('test.xls', data);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      相关资源
      最近更新 更多