我认为您的代码应该可以工作,但不要破坏请求!只需req.destroy();
从您的代码中删除行。为了不使用中间件,您也可以尝试这样的事情(它在我这边工作):
express = require 'express'
app = express()
swig = require 'swig'
cons = require 'consolidate'
swig.init {
root: __dirname + '/views'
allowErrors: true
}
app.set 'views', __dirname + '/views'
app.set 'view options', {layout: false}
app.engine '.html', cons.swig
app.set 'view engine', 'html'
# set upload dir
app.use express.bodyParser {uploadDir: './upload'}
app.get '/', (req, res) ->
res.render 'index.html', {}
app.post '/file-upload', (req, res) ->
# you can get some info about files here (size, path, name, type)
console.log req.files
# check that size is good
if req.files.thumbnail.size > 200
res.send 413
else
res.send 'Uploaded!'
console.log 'listen port 3000'
app.listen 3000
并以形式:
<form method="post" enctype="multipart/form-data" action="/file-upload">
<input type="file" name="thumbnail">
<input type="submit">
</form>
在这里查看详细信息。希望这会帮助你。