最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 2浏览 0评论

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

我想使用从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",
              "port": 443,
              "hostname": "testserver",
              "hash": null,
              "search": null,
              "query": null,
              "pathname": "/report/exportExcel",
              "path": "/report/exportExcel",
              "href": ""
            },
            "method": "POST",
            "headers": {
              "content-type": "multipart/form-data; boundary=--------------------------896095632834245509104518",
              "content-length": 1393
            }
          }
        }
回答如下:

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

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

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

我想使用从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",
              "port": 443,
              "hostname": "testserver",
              "hash": null,
              "search": null,
              "query": null,
              "pathname": "/report/exportExcel",
              "path": "/report/exportExcel",
              "href": ""
            },
            "method": "POST",
            "headers": {
              "content-type": "multipart/form-data; boundary=--------------------------896095632834245509104518",
              "content-length": 1393
            }
          }
        }
回答如下:

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

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

评论列表 (0)

  1. 暂无评论