如何将从http请求接收的.xls数据保存为node.js中的文件

问题描述 投票:0回答:1

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

我正在使用请求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个回答
0
投票

将encoding选项设置为null后,它可以工作

request(url, {encoding: null}, function(err, res, data) {
    if(err || res.statusCode !== 200) return;
    fs.writeFileSync('test.xls', data);
});
© www.soinside.com 2019 - 2024. All rights reserved.