文件上传 – 中止上传导致Sails js / Skipper崩溃

参考:
https://github.com/balderdashy/skipper/issues/49

适配器:skipper-gridfs

基本控制器代码:

req.file('fileTest')
        .upload({

        // You can apply a file upload limit (in bytes)
        maxBytes: maxUpload,
        adapter: require('skipper-gridfs'),
        uri: bucketConnect,
        saveAs : function (__newFileStream,cb) {
            cb(null, __newFileStream.filename);
        }

    }, function whenDone(err, uploadedFiles) {
        if (err) {
            var error = {  "status": 500, "error" : err };
            return res.serverError(error);
        }else {

我有一个jQuery-File-Upload客户端(https://blueimp.github.io/jQuery-File-Upload/)通过使用此处描述的jqXHR abort来强制执行“取消”过程(https://github.com/blueimp/jQuery-File-Upload/wiki/API):

$('button.cancel').click(function (e) {
    jqXHR.abort();
});

客户端中止后,服务器崩溃并显示以下消息:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: Request aborted
    at IncomingMessage.onReqAborted (.../node_modules/sails/node_modules/skipper/node_modules/multiparty/index.js:175:17)
    at IncomingMessage.EventEmitter.emit (events.js:92:17)
    at abortIncoming (http.js:1911:11)
    at Socket.serverSocketCloseListener (http.js:1923:5)
    at Socket.EventEmitter.emit (events.js:117:20)
    at TCP.close (net.js:466:12)

我已经使用了try / catch但它没有用,服务器无论如何崩溃了.

我不确定这是一个船长问题还是多党派问题 – 我的知识在这里停止(https://github.com/andrewrk/node-multiparty/blob/master/index.js):

function onReqAborted() {
    waitend = false;
    self.emit('aborted');
    handleError(new Error("Request aborted"));
  }

  function onReqEnd() {
    waitend = false;
  }

  function handleError(err) {
    var first = !self.error;
    if (first) {
      self.error = err;
      req.removeListener('aborted', onReqAborted);
      req.removeListener('end', onReqEnd);
      if (self.destStream) {
        self.destStream.emit('error', err);
      }
    }

    cleanupOpenFiles(self);

    if (first) {
      self.emit('error', err);
    }
  }

起初我认为这是jqXHR请求中止的方式,但它似乎是关于中止上传的通用Skipper问题,因为在上传期间关闭选项卡的简单操作将使服务器崩溃(不同的消息):

_stream_writable.js:233
    cb(er);
    ^
TypeError: object is not a function
    at onwriteError (_stream_writable.js:233:5)
    at onwrite (_stream_writable.js:253:5)
    at WritableState.onwrite (_stream_writable.js:97:5)
    at Writable.<anonymous> (.../node_modules/skipper-gridfs/index.js:179:25)
    at Writable.g (events.js:180:16)
    at Writable.EventEmitter.emit (events.js:117:20)
    at PassThrough.<anonymous> (.../node_modules/skipper-gridfs/index.js:194:36)
    at PassThrough.g (events.js:180:16)
    at PassThrough.EventEmitter.emit (events.js:117:20)
    at .../node_modules/sails/node_modules/skipper/standalone/Upstream/prototype.fatalIncomingError.js:55:17

我已经尝试通过在使用简单的上传控制器(而不是Skipper)时关闭选项卡来中止上传,并且没有崩溃:

var uploadFile = req.file('fileTest');
    console.log(uploadFile);

    uploadFile.upload(function onUploadComplete (err, files) {                // Files will be uploaded to .tmp/uploads

        if (err) return res.serverError(err);                              // IF ERROR Return and send 500 error with error

        console.log(files);
        res.json({status:200,file:files});
    });

所以,有没有人看到这种情况发生,有没有解决办法?

最佳答案 此问题已在skipper@0.5.4和skipper-disk@0.5.4中解决

参考:https://github.com/balderdashy/skipper/issues/49

点赞