[英]Multipart form data post method using express js
I tried to post form data to my express js server side.我试图将表单数据发布到我的 express js 服务器端。 This is my
pug
file这是我的
pug
文件
form(action="/profile" method="post" enctype="multipart/form-data")
input(type="file" accept="image/*" name="profileimage")
input(type="text" name="username")
input(type="submit" value="Upload")
After that I tried to log the post data with req.body
inside server side js as follow.之后,我尝试在服务器端 js 中使用
req.body
记录发布数据,如下所示。 That is my profile.js
那是我的
profile.js
router.post('/', function (req, res) {
console.log('Body- ' + JSON.stringify(req.body));
});
And this is my console result body- {}
这是我的控制台结果
body- {}
If I post without enctype="multipart/form-data"
like form(action="/profile" method="post")
, I can get the post data from my console result.如果我在没有
enctype="multipart/form-data"
情况下发帖,例如form(action="/profile" method="post")
,我可以从控制台结果中获取帖子数据。
You need to use a middleware to handle multipart form data, i think the most famous one is multer .您需要使用中间件来处理多部分表单数据,我认为最著名的是multer 。
You can find it here: https://github.com/expressjs/multer你可以在这里找到它: https : //github.com/expressjs/multer
To use it:要使用它:
First add it to your modules with npm install --save multer
in your root project首先在你的根项目中使用
npm install --save multer
将它添加到你的模块中
Then import it in your .js file var multer = require('multer');
然后将它导入到你的 .js 文件中
var multer = require('multer');
Choose your upload directory by setting the dest
argument in the multer
constructor: var upload = multer({ dest: 'uploads/' });
通过在
multer
构造函数中设置dest
参数来选择您的上传目录: var upload = multer({ dest: 'uploads/' });
Now just pass it as a middleware in your POST function as follows:现在只需将它作为中间件传递到您的 POST 函数中,如下所示:
router.post('/', upload, function (req, res) { console.log('Body- ' + JSON.stringify(req.body)); });
And don't forget to read the documentation at their github repo.并且不要忘记阅读他们 github 存储库中的文档。
express-formidable
module is best suited to send post data to the server I think.我认为
express-formidable
模块最适合将发布数据发送到服务器。 install express-formidable with following command npm install express-formidable
below is a sample code example使用以下命令
npm install express-formidable
下面是示例代码示例
let express = require('express');
let app = express();
let formidable = require('express-formidable');
let path = require('path');
app.use(formidable({
encoding: 'utf-8',
uploadDir: path.join(__dirname, 'uploads'),
multiples: true,
keepExtensions:true// req.files to be arrays of files
}));
app.post('/uploads',function(req,res){
console.log('Files '+JSON.stringify(req.files));// contains data about file fields
console.log('Fields '+JSON.stringify(req.fields));//contains data about non-file fields
});
you have to install path
module for this code to work with the following command npm install path
create a folder uploads in your project root directory.您必须为此代码安装
path
模块才能使用以下命令npm install path
在您的项目根目录中创建一个文件夹上传。 All the data related to file will be in req.files
and non-file data will be in req.fields
I have used it with ejs
templating engine and it would work just as fine with pug
.所有相关的文件中的数据将在
req.files
和非文件数据将在req.fields
我与用它ejs
模板引擎和它的工作就像用细pug
。 click This Link for further information about express-formidable
单击此链接以获取有关
express-formidable
更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.