Nodejs Express和文件上传
好吧,我已经尝试过使用 connect-form ,但由于某种原因我无法让它工作,但我想我应该从头开始理解它是如何工作的。
我不明白我上传的 multipart/formdata 文件要去哪里,或者当它发布到 url 时如何在我的应用程序中访问它。 -- 我想直接访问文件数据,并使用节点 fs 模块写入文件输出。 -- 例如:
app.post('/testy', function(req, res){
console.log(req.body);
console.log(req.headers);
res.redirect('back');
});
app.get('/testy', function(req, res){
res.send('<form method="post" action="/testy" enctype="multipart/form-data">'
+ '<p>Image: <input type="file" name="test" /></p>'
+ '<p><input type="submit" value="Upload" /></p>'
+ '</form>');
});
因此,实际记录的唯一 req var 是 req 标头,主体为空。 (可能应该是我理解的)。但我不明白的是文件数据在哪里?寻找我认为的 $_FILES 数组的 php 等效项。 -- 这是我记录的标题。
'accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'accept-language': 'en-us,en;q=0.5',
'accept-encoding': 'gzip,deflate',
'accept-charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'keep-alive': '115',
connection: 'keep-alive',
referer: 'http://127.0.0.1:3000/testy',
cookie: 'connect.sid=lDRpluTxjUJeuTmkXlybrYeZ.JYTB155s2DGce2dsyfv1Op5ISCY8uqyqJZK8NjlZ5jM; socketio=flashsocket',
'x-insight': 'activate',
'content-type': 'multipart/form-data; boundary=---------------------------5856401949371863571646035001',
'content-length': '30128' }
任何关于我所缺少的东西的线索都一如既往地受到高度赞赏!
Ok so ive already tried using connect-form and I couldnt get it working for some reason, but I figure I should understand how this works semi from scratch regardless.
I dont understand where the multipart/formdata file which I am uploaded is going, or how I can access it in my app when its posted to the url. -- Id like to access the file data directy, and write the file output using the node fs module. -- For instance:
app.post('/testy', function(req, res){
console.log(req.body);
console.log(req.headers);
res.redirect('back');
});
app.get('/testy', function(req, res){
res.send('<form method="post" action="/testy" enctype="multipart/form-data">'
+ '<p>Image: <input type="file" name="test" /></p>'
+ '<p><input type="submit" value="Upload" /></p>'
+ '</form>');
});
So the only req var that is actually being logged there is the req headers, body is empty. (probably supposed to be I understand that). But what I dont get is where is the file data? Looking for the php equiv of the $_FILES array I supposed. -- Here is my headers logged.
'accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'accept-language': 'en-us,en;q=0.5',
'accept-encoding': 'gzip,deflate',
'accept-charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'keep-alive': '115',
connection: 'keep-alive',
referer: 'http://127.0.0.1:3000/testy',
cookie: 'connect.sid=lDRpluTxjUJeuTmkXlybrYeZ.JYTB155s2DGce2dsyfv1Op5ISCY8uqyqJZK8NjlZ5jM; socketio=flashsocket',
'x-insight': 'activate',
'content-type': 'multipart/form-data; boundary=---------------------------5856401949371863571646035001',
'content-length': '30128' }
Any light shed upon what Im missing as always much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是没有连接形式的非常详细的版本。正如您所看到的,这效率不高,但试图对它的工作原理提供指导。
Here is very verbose version without connect-form. As you can see, this is not efficient but trying to be instructive about how it works.
从示例库运行此代码片段怎么样?
https://github.com/visionmedia/express/blob/master /examples/multipart/app.js
对我来说效果很好......
How about running this snippet from the example library?
https://github.com/visionmedia/express/blob/master/examples/multipart/app.js
works fine for me...
我终于能够让 connect-form 包工作了,菜鸟错误,但是,如果您使用的是 Express,请确保告诉应用程序在配置函数 app.configure(function(){ 中使用表单模块
app.use(form({ keepExtensions: true }));
(在帖子中它将位于 files.yourfileuploadfieldname.filename 变量中)
——话虽如此,我仍然有兴趣知道如何从头开始,没有连接形式,如果它不是非常难以解释的话。
I was able to get the connect-form package working finally, rookie mistake but, if you are using express make sure you tell the app to use the form module within your config function app.configure(function(){
app.use(form({ keepExtensions: true }));
(the in the post it will be in the files.yourfileuploadfieldname.filename variable)
-- with that said Im still interested to know how to do it from scratch, without connect-form, if its not incredibly difficult to explain.