how to detect whether file download successfully from client side in mean/angular js

为君一笑 提交于 2021-01-28 18:06:42

问题


I have created a chat application in which I have kept file sending functionality over chat windows. Now I want to remove files from the server end when clients successfully download them. I'm using the MEAN stack.

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); 
});

回答1:


You can use jQuery File Download plugin for your purpose, it's a simple example you can use in your client for manage the downloaded file:

$.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');
    });

Here there are more examples



来源:https://stackoverflow.com/questions/37782417/how-to-detect-whether-file-download-successfully-from-client-side-in-mean-angula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!