当我发送文件到/ upload时,从终端我可以看到代码被阻止在form.parse(req ...在互联网上我读到这是因为我使用bodyParser(),而且人们建议这样做:
delete express.bodyParser.parse['multipart/form-data'];
然而,当我这样做时,我的代码崩溃,因为parse为null。
任何人都知道如何让文件上传仅在/ upload中运行,并且可能还有强大的功能?
感谢。
这是我的 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);
};
答案 0 :(得分:3)
如果你想使用强大的,那么删除app.use(express.bodyParser());
&安培;添加app.use(express.json());app.use(express.urlencoded());
此解决方案对我有用,对其他开发者可能有所帮助。
答案 1 :(得分: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');
}