2013-02-06 74 views
0

當我從終端發送/上傳文件時,我可以看到代碼在form.parse處被阻止(請求...在Internet上,我讀取這是我用bodyParser(),和人建議要做到這一點:當在Nodejs中上傳文件時,Formidable doesnt form.parse()

delete express.bodyParser.parse['multipart/form-data']; 

然而,當我這樣做,因爲解析我的代碼崩潰是空

任何人有關於如何獲取文件的任何想法。上傳工作只在/上傳,可能與強大?

謝謝。

這是我app.js

'use strict'; 

var express = require("express"); 
var async = require("async"); 
var http = require('http'); 
var url = require("url"); 
var qs = require("querystring"); 
var fs = require("fs"); 
var formidable = require("formidable"); 
var mime = require("mime"); 

var app = module.exports = express(); 

app.configure(function() { 
    app.set("views", __dirname + "/views"); 
    app.set("view engine", "ejs"); 
    app.engine("html", ejs.renderFile); 
    app.use(express.bodyParser()); 
    app.use(express.methodOverride()); 
    app.use(express.cookieParser("abc")); 
    app.use(express.static(__dirname + "/static")); 
    app.use(app.router); 
    app.use(express.limit('5mb')); 
}); 

app.post('/upload', storage.upload); 

這是我的代碼storage.js

'use strict'; 

var async = require('async'); 
var formidable = require('formidable'); 
var fs = require('fs'); 
var util = require('util'); 
var api = require('./common'); 

exports.upload = function (req, res) { 

    console.log("I am here"); 

    var form = new formidable.IncomingForm(), 
    files = [], 
    fields = []; 

    form.uploadDir = "./uploads"; 

    form 
    .on('field', function (field, value) { 
     console.log(field, value); 
     fields.push([field, value]); 
    }) 
    .on('error', function (err) { 
     res.writeHead(200, {'content-type': 'text/plain'}); 
     res.end('error:\n\n'+util.inspect(err)); 
    }) 
    .on('file', function (field, file) { 
     console.log(field, file); 
     files.push([field, file]); 
    }) 
    .on('aborted', function (err) { 
     console.log("user aborted upload"); 
    }) 
    .on('end', function() { 
     console.log('-> upload done'); 
     res.writeHead(200, {'content-type': 'text/plain'}); 
     res.write('received fields:\n\n '+util.inspect(fields)); 
     res.write('\n\n'); 
     res.end('received files:\n\n '+util.inspect(files)); 
    }); 
    form.parse(req); 
}; 

回答

1

你見過在GitHub上的例子:https://github.com/visionmedia/express/blob/master/examples/multipart/index.js

我已將其修改爲在Express 3.x上運行,並且似乎順利運行:

var express = require('express'); 
var fs = require('fs'); 
var app = express(); 
var format = require('util').format; 


// bodyParser in connect 2.x uses node-formidable to parse 
// the multipart form data. 
app.use(express.bodyParser()) 

app.get('/', function(req, res){ 
    res.send('<form method="post" enctype="multipart/form-data">' 
    + '<p>Title: <input type="text" name="title" /></p>' 
    + '<p>Image: <input type="file" name="image" /></p>' 
    + '<p><input type="submit" value="Upload" /></p>' 
    + '</form>'); 
}); 

app.post('/', function(req, res, next){ 
    // the uploaded file can be found as `req.files.image` and the 
    // title field as `req.body.title` 
    res.send(format('\nuploaded %s (%d Kb) to %s as %s' 
    , req.files.image.name 
    , req.files.image.size/1024 | 0 
    , req.files.image.path 
    , req.body.title)); 
}); 

if (!module.parent) { 
    app.listen(3000); 
    console.log('Express started on port 3000'); 
} 
+0

以這種方式對所有app.post(「*」)我將文件上傳到我的服務器上,我想避免的事情 – nick

+0

是的,這只是上傳文件功能的一個工作示例。我並不是要你逐字使用它,而是要將它與你的代碼進行比較,看看上傳是如何在一個而不是另一個上進行的。 –

3

如果你想使用強大的話,刪除app.use(express.bodyParser()); &添加app.use(express.json());app.use(express.urlencoded());

Connect 3.0

該解決方案爲我工作,它可能是其他開發很有幫助。