gpt4 book ai didi

javascript - 如何在mean/angular js中检测文件是否从客户端下载成功

转载 作者:太空宇宙 更新时间:2023-11-04 00:38:45 25 4
gpt4 key购买 nike

我创建了一个聊天应用程序,在其中保留了通过聊天窗口发送文件的功能。现在我想在客户端成功下载文件时从服务器端删除文件。我正在使用 MEAN 堆栈。

router.post("/postChatFileSend",ensureAuthenticatedForPost,function(req,res) {

if (req.body.fileName)
{
var filePath = __dirname + '/../public/ChatFile/'+req.body.fileName;
fs.unlink(filePath, function (err) {
})
};

return res.json({"status": true,"messages":"messages read sucessfully"});
})

router.get('/downloadChatFile/:fileName',ensureAuthenticatedForPost, function(req, res) {

var file = __dirname + '/../public/ChatFile/'+req.params.fileName;
res.download(file);
});

最佳答案

您可以使用jQuery File Download插件适合您的目的,这是一个简单的示例,您可以在客户端中使用它来管理下载的文件:

$.fileDownload('urlForYourFile')
.done(function () {
alert('File download a success!');
$.post('/postChatFileSend', {fileName: 'fileName'}, function(data) {
//Check the response, if the status propery is true, the file must have been removed from the server
});
})
.fail(function() {
alert('An error has ocurred');
});

这里有more examples

关于javascript - 如何在mean/angular js中检测文件是否从客户端下载成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37782417/

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