我在express3.x并使用强大的
app.post "/up",(req,res)->
formidable = require('formidable')
form = new formidable.IncomingForm()
form.uploadDir = __dirname + "/uploads"
form.encoding = 'binary'
form.addListener 'file',(name,file) ->
#write file
console.log('file uploaded')
return
console.log('$$$$$$$$$$$')
form.addListener 'end', ->
console.log('end')
res.end()
return
console.log('@@@@@$$$$')
form.parse req,(err,fields,files)->
console.log('parse')
console.log(err) if(err)
return
console.log('######')
return
,上传表单为
block content
:coffeescript
$ ->
formData = new FormData()
xhr = new XMLHttpRequest()
onProgress = (e)->
per_complete = (e.loaded/e.total) * 100 if e.lengthComputable
onReady = (e)->
alert("Ready")
onError = (err)->
alert("Error Loading "+err)
$('#upload').click (e)->
formData.append('img',document.getElementById('img').files[0])
xhr.open('post','/up',true)
xhr.addEventListener('error',onError,false)
xhr.addEventListener('progress',onProgress,false)
xhr.send(formData)
xhr.addEventListener('readystatechange',onReady,false)
h1 hello world
form
|select Image:
input(type='file',name='img', id='img')
input(type='button',value='button', id='upload')
没有任何事件被触发......不确定我错过了什么..
答案 0 :(得分:3)
如果您使用app.use(express.bodyParser())
相当于:
app.use(express.json());
app.use(express.urlencoded());
app.use(express.multipart());
您可以删除app.use(express.multipart());
,然后就可以使用强力对象。
答案 1 :(得分:2)
express在内部使用强大的,所以你的强大不会得到任何事件,如果你想自己使用强力,你必须从bodyParser中删除multipart/form-data
我仍然使用快递2.x但是这也应该在你的app.configure函数中使用3.x,然后
app.configure(function(){
delete express.bodyParser.parse['multipart/form-data']
})
现在你可以自己使用强大的。
答案 2 :(得分:1)
您使用强力而不是内置bodyParser的具体原因是什么?它使用multipart中间件并基于强大的。因此,formidable中的大多数选项也可以应用于bodyParser。例如:
app.use(connect.multipart({ uploadDir: path }));
要回答您的问题,请尝试以下代码:
app.js
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.post("/", function(req, res) {
console.log(req.files.img)
res.end()
})
index.jade
form
input(type="file", name="img", id="img")
input(type="button", value="button", id="upload")
script
var formdata = new FormData()
var xhr = new XMLHttpRequest()
$("#upload").on("click", function(e) {
xhr.upload.onprogress = function(evt) {
console.log("While sending and loading data.")
if (evt.lengthComputable) {
console.log (evt.loaded / evt.total * 100);
}
}
xhr.onloadend = function(evt) {
console.log("When the request has completed (either in success or failure).")
}
xhr.onload = function(evt) {
console.log("When the request has successfully completed.")
}
var image = document.getElementById('img').files[0]
formdata.append("img", image)
xhr.open("POST", "/", true)
xhr.send(formdata)
})
查看W3C Specifications了解更多活动。
答案 3 :(得分:1)
我遇到了同样的问题,在我的控制器完成处理之前,文件是完全上传的。事件被解雇但我还没有听。 为了反击行为,我编写了一个中间件来捕获文件上传并保留它们以供控制器稍后访问。
https://gist.github.com/3813103
您可以使用
实现它var fileHandler = require('./middleware/fileHandler');
app.use(fileHandler());
答案 4 :(得分:1)
Nodejs/快速设置
// https://www.npmjs.com/package/express-formidable
npm install express-formidable
快速演示
const express = require('express');
const formidableMiddleware = require('express-formidable');
const app = express();
// Express-Formidable attaches your multi-part form to the request object
app.use('/formdata/with-fields-and-files', formidableMiddleware(), (req, res) => {
console.log(req.fields, 'fields')
console.log(req.files, 'files')
}
app.listen(3000)
答案 5 :(得分:0)
今天发生了类似的问题。
浪费了4个多小时的时间。
在这里期待得到一个答案,但是在这里找不到它,所以写它。
问题
1. formidable - fileBegin event is fired
2. formidable - file event is fired - for small ~3MB/~4MB files
3. formidable - file event is not fired - for large files
on local machine - nodejs/libraries will work fine
on remote machine - behind nginx it above issue happens
使用的库:
-微型-https://github.com/zeit/micro-编程模型过于简单的罪魁祸首
-强大-https://www.npmjs.com/package/formidable-罪魁祸首没有提供异步/等待的示例
解决方案
1. increase the VM memory size 2/3 times the file upload size (libraries eat a lot of memory)
2. increase both proxy & http timeout/ size in the nginx to the required
- example, 10 mins timeout & 2G size
3. change the programming model for large files
a. on local machine - nodejs/libraries will work fine
b. but behind the nginx proxy, it optimizes and kills the on going process asap
to solve this proxy issue, use more promises/ async-awaits, delay the reply to nginx
希望有帮助。