5
    var xls = json2xls(data);
    var bufferdata = new Buffer(xls).toString('base64');
    console.log("inside upload s3 function........", data);
    //var buf = new Buffer(data, 'base64');
    var uploaddata = {
        Key: "exception.xls",
        Body: bufferdata,
        ContentEncoding: 'base64',
        ContentType: 'application/vnd.ms-excel'
    };

我能够通过 s3 保存和检索文件。但是格式不匹配即将出现。请帮助我解决这个问题。

4

2 回答 2

1

试试这个,对我有用。

const xls = json2xls(data);

const buffer = Buffer.from(xls, 'binary');

console.log("inside upload s3 function........", data);
var uploaddata = {
    Key: "exception.xls",
    Body: buffer,
    ContentType: 'application/vnd.ms-excel'
};
于 2019-09-26T04:36:56.853 回答
0

Sameer解决方案非常适合 xlsx/xls 文件类型。

对于 CSV。我使用了 json2csv包。

const { Parser } = require('json2csv');
const fields = ['id', 'title', 'description'];
const opts = { fields };
const parser = new Parser(opts);
const csv = parser.parse(your data);

const uploaddata = {
Key: "exception.csv",
Body: csv,
ContentType: 'application/vnd.ms-excel'
};

快乐编码;)

于 2019-09-26T11:27:12.823 回答