gpt4 book ai didi

node.js - 如何在 NodeJS/Multer 中以相同的形式发送字段和文件?

转载 作者:搜寻专家 更新时间:2023-11-01 00:20:05 24 4
gpt4 key购买 nike

我的问题是当我发布到/new 时无法获取 req.body 和 req.file 对象,但一切正常,文件正在上传到我的文件夹中。我只是无法访问 req.body 和 req.file 对象。当我在控制台中记录 req.body 时,它返回 [object Object],对于​​ req.file,它记录未定义。

我做了一些研究,发现您无法使用 body-parser 对 multipart/form-data 进行编码,但我需要它,因为没有它我无法发送文件。

var router = require('express').Router(),
multer = require('multer'),
path = require('path'),
passport = require('passport'),
middleware = require('../middleware'),
User = require('../models/User'),
Ad = require('../models/Ad');

// Storage setup
var storage = multer.diskStorage({
destination: './public/uploads/',
filename: function(req, file, done) {
done(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});

// Upload setup
var upload = multer({
storage: storage,
limits: {
fileSize: 3000000
}
}).single('banner');

// Check file type
function checkFileType(file, callback) {
// Allowed extensions
var fileTypes = /jpeg|jpg|png|gif/;
// Check extention
var extname = fileTypes.test(path.extname(file.originalname).toLowerCase());
// Check mime type
var mimeType = fileTypes.test(file.mimetype);

if (mimeType && extname) {
return callback(null, true);
}
callback(null, false);
}

router.get('/new', middleware.isLoggedIn, function(req, res) {
res.render('new');
});

router.post('/new', middleware.isLoggedIn, function(req, res) {
var adObj = {
owner: req.user,
url: req.user.url,
paymentType: req.body.paymentType,
transactionId: '',
banner: ''
};

if (adObj.paymentType === 'paid') {
adObj.transactionId = req.body.transactionId;
}

upload(req, res, function(err) {
if (err) {
req.flash('error', err.message);
return res.redirect('/new');
}
adObj.banner = '/uploads/' + req.file.filename;
});

var ad = new Ad(adObj);
ad.save();

req.flash('success', 'Successfully added new ad.');
res.redirect('/');
});
<form class="main-form" action="/new" method="POST" enctype="multipart/form-data">
<input type="url" name="url" placeholder="Website URL" required>
<select class="select-ad-type" name="paymentType">
<option value="free" selected>Free Ad</option>
<option value="paid">Sponsored Ad - $10</option>
</select>
<div class="transaction-section">
<p>Send $10 to example@gmail.com and type your transaction id below. Your ad will be displayed in the "SPONSORED ADS" section in less than 24 hours.</p>
<input type="text" name="transactionId" placeholder="Transaction Id">
</div>
<input type="file" name="banner" required>
<button><i class="fa fa-plus" aria-hidden="true"></i> SUBMIT</button>
</form>

最佳答案

您收到错误是因为您试图在 multer 进行实际解析之前访问这些值。您正确地指出 body-parser 不适用于 multipart-data,但在 multipart-data 表单类型中,multer 可以解析 fileform 中的其他字段,这就是为什么我们可以同时访问 req.file请求正文

确保在 upload(req, res, function(err) {//req.body and req.file })

中访问它

此外,还有一个与异步代码相关的问题。

解决方法:

router.post('/new', middleware.isLoggedIn, function(req, res) {
upload(req, res, function(err) {
if (err) {
req.flash('error', err.message);
return res.redirect('/new');
}

var adObj = {
owner: req.user,
url: req.user.url,
paymentType: req.body.paymentType,
transactionId: '',
banner: ''
};

if (adObj.paymentType === 'paid') {
adObj.transactionId = req.body.transactionId;
}

adObj.banner = '/uploads/' + req.file.filename;

var ad = new Ad(adObj);
ad.save();

req.flash('success', 'Successfully added new ad.');
return res.redirect('/');
});
});

关于node.js - 如何在 NodeJS/Multer 中以相同的形式发送字段和文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48732027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com