5

我们目前在尝试使用移动浏览器时遇到图像上传挂起的问题,但在使用开发计算机时却没有。我们认为 express.bodyParser 中间件是罪魁祸首,因为它没有访问我们服务器中的代码。我们正在使用节点 0.10.11 并表示 3.2.5

这是相关的服务器代码

// --- Server Setup --- \\  
var server = express();

// all environments
server.set('port', process.env.PORT || 3000);
server.engine('ejs', engine);
server.set('views', __dirname + '/views');
server.set('view engine', 'ejs');
server.use(express.favicon());
server.use(express.logger('dev'));
server.use(express.json())
server.use(express.urlencoded())
server.use(express.methodOverride());
server.use(express.cookieParser('your secret here'));
server.use(express.session());
//Passport stuff
server.use(passport.initialize());
server.use(passport.session());

server.use(express.static(path.join(__dirname, 'public')));
server.use(server.router);

// development only
if ('development' == server.get('env')) {
  server.use(express.errorHandler());
}

// This is the problem route, mw.validateUID just validates the uid and we know that is working 
// where as it seems to stop working at the express.bodyParser
server.post('/photo/:uid', mw.validateUID, express.bodyParser({'keepExtensions': true}), express.limit('2mb'), app.uploadPhoto);

相关调用代码

RestClient = function() { 
    var xhr = new XMLHttpRequest();

    function init() {
      return {
         postImage: function(path, image, callback) {
           if (image.size > 2500000) {
              callback(-1);
              return;
           }

           var formData = new FormData();               
           formData.append('photo', image);

           xhr.open('POST', path, true);
           xhr.send(formData);
           xhr.onreadystatechange = function() {
             if (this.readyState == this.DONE) {
               callback(this.status);
              } 
           };
         }
       }
}

processImage = function(imageFile) {
  if (user.getId()) {
    restClient.postImage('/photo/' + user.getId(), imageFile, function(status) {
      if (status < 0) {
        alert("Your image must be less than 2.5M in size.");
      } else if (status == 200) {
        reset(true);
        if (!successView) {
          successView = SuccessView();
        }
        successView.show();
      } else {
        alert("Sorry, we're unable to upload your photo.  Please try again later.");
      }                 
    });
  } else {
    alert('login to fb!');
  }
}

编辑:

这也是我们遇到的错误消息。

Error: Request aborted
    at IncomingMessage.<anonymous> (/home/pinnacle_vodka/node_modules/express/node_modules/connect/node_modules/formidable/lib/incoming_form.js:107:19)
    at IncomingMessage.EventEmitter.emit (events.js:92:17)
    at abortIncoming (http.js:1881:11)
    at Socket.serverSocketCloseListener (http.js:1893:5)  
    at Socket.EventEmitter.emit (events.js:117:20)
    at TCP.close (net.js:466:12)
4

1 回答 1

1

问题在于您的快递应用程序的中间件。

您使用过:

server.use(express.bodyParser());

它告诉 Express 处理 JSON、URLEncoded 和多部分请求。但是,似乎也有一个节点强大的模块来处理多部分请求。解决方案是要么仅使用 URLEncoded 并以强大的方式处理上传,要么不使用强大的并处理 bodyparser 中的所有内容。

所以您的代码现在应该如下所示:

a) 强大的上传:

server.use(express.urlencoded());
//server.use(express.methodOverride());
//server.use(express.bodyParser());

或 b) 快速分段上传:

//server.use(express.urlencoded());
//server.use(express.methodOverride());
server.use(express.bodyParser());

代码中有对某处的引用node_modules/formidable/lib/incoming_form.js:107:19,如果您使用选项 b,请将其删除。

于 2014-01-28T06:10:58.783 回答