我创建了一个聊天应用程序,在其中保留了通过聊天窗口发送文件的功能。现在我想在客户端成功下载文件时从服务器端删除文件。 我正在使用 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');
});
关于javascript - 如何在mean/angular js中检测文件是否从客户端下载成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37782417/