簡體   English   中英

解析node.js Express應用程序,形式為enctype =“ multipart / form-data”銷毀請求主體?

[英]Parse node.js express app, form enctype=“multipart/form-data” destroys request body?

我正在使用解析功能構建應用程序后端,我需要上傳一些圖像,但是當我使用enctype形式為multipart / form-data的形式發布到應用程序網址時,主體為空?

這是收到帖子的方法:

// These two lines are required to initialize Express in Cloud Code.
var express = require('express');
var app = express();

// Global app configuration section
app.set('views', 'cloud/views');  // Specify the folder to find templates
app.set('view engine', 'ejs');    // Set the template engine
app.use(express.bodyParser());    // Middleware for reading request body

app.post('/add_station', function(req, res) {
       console.log(JSON.stringify(req.body));
       res.send(JSON.stringify(req.body));

});

當表單類型為multipart / form-data時,req.body為空?

如果您使用的是Express 4.0或更高版本,則它不再與中間件捆綁在一起。 這意味着您需要手動安裝它們。

Body-parser也不再適用於文件上傳,因此您需要使用multer之類的東西

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var multer  = require('multer')

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(multer({
    dest: './uploads/'
}))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM