gpt4 book ai didi

angularjs - 如何在 Angular $http 中处理 Express response.write() 的响应

转载 作者:太空宇宙 更新时间:2023-11-03 22:14:11 26 4
gpt4 key购买 nike

我正在尝试使用 ng-file-upoad 上传 csv 文件。这是我的代码片段:

Upload.upload({
url: baseUrl + '/file-upload',
data: {
file: file
}
})
.then(function(res) {
console.log('success: ===> ', res);
}, function(err) {
console.log('erroir: ===> ', err);
}, function() {
console.log('progress: ', arguments);
});

在 Node 环境中,我正在解析文件并将数据插入数据库中。我不想关闭连接。这就是我使用“response.write”的原因。这是我的代码片段:

var path = req.files.file.path,
currentIndex = 0;

fs.readFile(path, 'utf8', function(err, data) {
if(err) {
// handle error
} else {
// making array (dataArray) from data
dataArray.forEach(function(eachData){
newEntry = new app.db.models.SomeCollection(eachData);
newEntry.save(function(err, data) {
if (currentIndex === dataArray.length) {
res.end('DONE!');
} else {
currentIndex++;
res.write(JSON.stringify({
total: dataArray.length,
done: currentIndex
}));
}
});
})
}
});

我的问题是如何获取在“res.write”中传递的数据?我不想仅将套接字用于此目的。我错过了什么吗?

最佳答案

如前所述 here :

response.send(msg) is equal to response.write(msg);response.end();

Which means, send can only be called once, write can be called many times, but you must call end yourself.

您可能没有收到响应,因为缺少 response.end()

一旦您 end() 您的响应,您应该能够在返回的 Upload.upload promise 中访问 Angular Controller 中的响应数据。

这不像您所说的关闭连接。这不是类似套接字的实现(例如 wssocket.io)。一旦发出请求,即使要提供有关该请求的错误详细信息(即状态 401、403、404 等),它也应该有响应。

关于angularjs - 如何在 Angular $http 中处理 Express response.write() 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34333830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com