7

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

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如果用户未登录,我想结束此上传过程并返回成功 = 0。这不起作用。在客户端,请求一直挂起,没有任何响应。

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

//---> 3我想在进度上使用第三个错误来创建进度条。但我很快遇到了一些问题。首先,在进度上使用会大大降低系统速度。也会导致第四步出错。

第四个错误//---> 4如果我们从步骤 3 中删除 on('progress'),这将按预期工作。完成上传后,它会向客户端返回成功 = 1。但是,当 on('progress')return res...在 step中出现时//---> 4不起作用,并且客户端请求再次保持挂起而没有任何响应。

几个问题:如果 on('progress') 不存在,为什么下面的代码在//---> 1它工作时不能工作//---> 4

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

为什么 on 进度会大大减慢上传过程?

顺便说一句,在我的客户端我使用 form.js 插件。因此我的请求如下所示:

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

1 回答 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,
                });
            }
        });
    }
},

这仍然不是最好的解决方案。我实际上发现其中一些很hacky。如果有人有更好的解决方案,请分享。

于 2015-04-09T05:50:23.563 回答