图像上传挂在移动浏览器上但不在本地计算机上 - 节点JS,Express

时间:2013-06-19 20:47:23

标签: node.js express

我们目前正在尝试使用移动浏览器而不是在使用开发计算机时挂起的图片上传。我们认为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)

1 个答案:

答案 0 :(得分:1)

麻烦在于你的快递应用程序的中间件。

您使用过:

server.use(express.bodyParser());

告诉Express处理JSON,URLEncoded和多部分请求。但是,似乎还有一个节点Formidable模块来处理多部分请求。解决方案是要么只使用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,请将其删除。