在sails.js中使用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如果用户没有login,我想结束这个上传过程,并返回成功= 0。这是行不通的。 在客户端,请求保持悬挂而没有任何回应。

第二个错误//---> 2我遇到了一个错误,如前所述https://github.com/balderdashy/skipper/issues/36 ,所以作为一个快速解决scheme,我使用了在github的评论中使用的人。 但是和问题1一样,我遇到了这个问题。 如果文件大小超过MAXBYTES,我想结束这个上传过程,并返回成功= 0给用户。 这不是回到客户端。

第三个错误//---> 3我想用进度来创build一个进度条。 但是我很快遇到了一些问题。 首先,使用进度会减慢系统的速度。 也会导致第四步中的错误。

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

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

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

为什么进行中的上传过程太慢?

btw在我的客户端我使用form.js插件。 因此,我的要求是这样的:

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

我花了一些时间来解决这个问题,到了那个时候,我已经完全忘记了我的问题了。 这些是我为完成这项工作而采取的一些步骤。

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

这仍然不是最好的解决scheme。 实际上我发现它有些黑客。 如果有人有更好的解决scheme,请分享。