当尝试用nodejs将文件上传到服务器时,req.files为空
req.files is empty when trying to upload a file to server with node js
HTML:
<form action="/uploadpic" method="post" enctype="multipart/form-data">
<input type="file" data-clear-btn="true" name="image" id="new_pic" value="" placeholder="Choose File">
<input type="submit" value="Add" style="width:30%">
</form>
NodeJS:
app.post('/uploadpic', function(req,res) {
console.log(req.files);
console.log(req.body);});
我也使用:
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded());
app.use(bodyParser.json())
app.use(express.bodyParser({uploadDir:'./uploads'}));
app.use(bodyParser.json({ type: 'application/vnd.api+json' }))
在控制台中我得到:
{}
{}
我似乎不明白这里可能出了什么问题。。谢谢
var fs = require('fs');
app.post('/uploadpic', function(req,res) {
//req.files contains array of files iterate and get it
//if it has only one. it is like object
//here is the code for object
if (req && req.files) {
var contentType = req.files.file.type;
var fname = req.files.file.name;
var image_path = req.files.file.path;
fs.readFile(image_path, function (err, data) {
var data = data; //this is your data use this
})
}
})
BodyParser不包括文件上传。你需要使用类似multer或multiparty的东西。
此外,express(4.0+(不再与中间件捆绑在一起,因此您需要对POST请求使用bodysparser。
相关文章:
- 使用JSP从服务器检索和显示图像
- 如何使用skip参数使用angular ui引导进行服务器端分页
- 客户端服务器REST API captcha实现
- 正在将base64 jpeg从input-type=file上传到服务器
- Webpack开发服务器和React服务器端渲染
- 提示使用服务器端事件处理程序激活JavaScript
- 使用谷歌应用程序脚本将服务器端数据表返回到客户端
- 如何使用Socket.io将命令从客户端发送到服务器
- jQuery blueimp文件上传:将N-1个文件上传到IE中的服务器
- 使用jasmine模拟对服务器的调用
- 如何轻松地将服务器端变量从Java代码转移到客户端代码
- 从客户端获取修改后的对象,并将其与服务器上的原始对象组合
- 通过ajax将坐标传递到php服务器端,并在处理后检索到javascript
- 如何使用Javascript将空数组发送到PHP服务器
- 如何在HTML中显示服务器对象变量
- 如何配置分析以将数据发送到我自己的服务器
- req.user在使用express服务器passport.js进行twitter身份验证后未定义
- req.isAuthenticated()在服务器启动一段时间后返回false
- 当尝试用nodejs将文件上传到服务器时,req.files为空
- 如何在快速节点服务器中获取“req”的参数