【发布时间】:2018-10-28 10:42:34
【问题描述】:
我有一个 NodeJS 后端,它使用 json 包侦听 POST 请求。它使用 json 包中的信息构建一个大约 15,000 行的 csv 文件,然后使用 res.download 将 csv 文件发送回客户端。
从云数据库读取到 csv 文件没有问题。我已经检查了服务器中的文件,并且这些行都在那里并且它们是准确的。但是,在客户端下载的文件可能有几百行被截断。似乎 res.download() 运行得太快了,即使我已经明确地将流设置为在 for 循环结束后结束,或者它在应该运行时运行但 csv 文件仍在缓冲或其他东西
这是我的代码:
服务器端:
app.post('/dashboard/download_data', function (req, res) {
let payload = req.body;
ref.orderByKey().once("value", function (snapshot) {
let data = snapshot.val();
writer.pipe(fs.createWriteStream('C:\\user\\EVCS_portal\\out.csv'));
for (let key in data) {
if (data.hasOwnProperty(key)) {
test_time = data[key]['time'];
writer.write({
time: data[key]['time'],
ac2p: data[key]['ac2p'],
dcp: data[key]['dctp']
})
}
}
writer.end('This is the end of writing\n');
writer.on('finish', () => {
console.log(test_time);
res.download('C:\\user\\EVCS_portal\\out.csv');
console.log('file sent out!')
});
})
客户端js:
firebase.auth().currentUser.getIdToken(true).then(function (idToken) {
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
let a = document.createElement('a');
a.href = window.URL.createObjectURL(xhr.response);
a.download = download_date + '.csv';
a.style.display = 'none';
document.body.appendChild(a);
a.click();
}
};
let url = "/dashboard/download_data";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.responseType = 'blob';
// Package our payload including the idToken and the date
let data = JSON.stringify({"idToken": idToken, 'date': download_date});
xhr.send(data);
【问题讨论】:
标签: javascript node.js