使用Sails.js中的Skipper处理上传文件(进行中)

7

我正在使用Skipper将多个文件一次性上传到本地文件夹。但是我遇到了一些问题。

upload: function (req, res) {
    if (_.isEmpty(req.session.User)){
        return res.json({                                       //---> 1
                    success: 0
               });
    }else{
        res.setTimeout(0);
        var MAXBYTES = 10*1000*1000;

                                                                //---> 2
        if (req._fileparser.form.bytesExpected > MAXBYTES){
            return res.json({
                success: 0,
                error: 'File size limit exceeded.'
            });
        }else{

            req.file('file[]').on('progress', function(event){
                return event;                                   //---> 3
            }).upload({

                maxBytes: MAXBYTES

            }, function whenDone(err, uploadedFiles) {
                                                                //---> 4
                    return res.json({
                        success: 1,
                    });

            });
        }
    }
},

第一错误 //---> 1 如果用户未登录,我希望结束上传过程并返回 success = 0。但是这行不通。在客户端,请求被挂起而没有任何响应。

第二错误 //---> 2 我之前遇到了一个错误,如此描述:https://github.com/balderdashy/skipper/issues/36,因此作为一个快速修复,我使用了github评论中的某些内容。但是像问题1一样,我遇到了这个问题。如果文件大小超过MAXBYTES,则我希望结束此上传过程并将success = 0返回给用户。请求不会回到客户端。

第三错误 //---> 3 我想使用 on progress 来创建进度条。但我很快遇到了一些问题。首先,使用 on progress 会使系统变得太慢。此外,它会导致第4步出错。

第四错误 //---> 4 如果我们从第3步中删除 on('progress'),则此代码将按预期工作。完成上传后,它将向客户端返回 success = 1。然而,当 on('progress') 存在时,步骤 //---> 4 中的return res... 不起作用,客户端请求再次被挂起而没有任何响应。

几个问题: 为什么在 //---> 4 如果 on ('progress') 不存在,以下代码能正常工作,但在 //---> 1 中就不行。

return res.json({
   success: 0
});

为什么on progress会严重拖慢上传过程?

顺便提一下,我在客户端使用form.js插件。因此我的请求看起来像这样:

$('#upload').ajaxForm({
    uploadProgress: function(event, position, total, percentComplete){
        console.log(percentComplete);
    },
    success: function(data) {
        console.log(data);
    }
});
1个回答

5

我花了一些时间才解决了这个问题,等到我解决的时候,我完全忘记了我在stackoverflow上的问题。以下是我采取的一些步骤使其工作。

upload: function (req, res) {
    if (_.isEmpty(req.session.User)){
        return res.json({   // or destroy connection as shown below
            success: 0
        });
    }else{
        res.setTimeout(0);
        var MAXBYTES = 10*1000*1000;

        if (req._fileparser.form.bytesExpected && req._fileparser.form.bytesExpected > MAXBYTES) {
            // file size exceeded //-> also check filesize on client-size before uploading because this will not send error report back to the client
            req.connection.destroy();
        }

        req.file('file[]').on('progress', function(event){
            // returning anything here was unnecessary
            // For example jQuery form plugin's client-side `uploadProgress:` will still catch the progress
        }).upload({
            maxBytes: MAXBYTES
        }, function whenDone(err, uploadedFiles) {
            var tempFileNames = _.pluck(uploadedFiles, 'fd');
            if (_.isEmpty(tempFileNames)) {
                return res.json({
                    success: 0,
                    error: '0 files selected.'
                });
            }else{
                // process upload
                return res.json({
                    success: 1,
                });
            }
        });
    }
},

这仍然不是最好的解决方案。我实际上认为其中一些方法很粗糙。如果有更好的解决方案,请分享。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接