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

标签 javascript angularjs node.js mongodb express

我创建了一个聊天应用程序,在其中保留了通过聊天窗口发送文件的功能。现在我想在客户端成功下载文件时从服务器端删除文件。 我正在使用 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/

相关文章:

javascript - 对两个数组进行分组

javascript - AngularJS - 共享服务对象被错误删除

angularjs - bootstrap css 版本 3.3.2 未显示 Angular 引导模式背景

javascript - 不允许添加除定义之外的值。 MongoDB

node.js - 如何仅在使用特定路由时在 Koa 中指定静态文件夹?

javascript - html 中的 onmouser 图像

javascript - 如何给 ul 标签元素添加 onchange 事件?

javascript - pixi.js pivot 影响对象位置

angularjs - 如何使用 MEAN 和sails.js 开始一个新项目

node.js - Redis: 'Get - Check Condition - Increase' 带锁定 key