Multer文件上传错误(nodejs和react)
Multer file upload error (nodejs and react)
我已经从客户那里发送了这个请求。
if(file){
const data = new FormData()
const fileName = Date.now() + file.name
data.append('file', file) // file comes from state while submitting
data.append('name', fileName)
try {
await axios.post('/upload_image', data)
} catch (error) {
console.log(error.message);
}
}
节点
const multer = require('multer')
// file upload
const storage = multer.diskStorage({
destination: (req, file, cb) => {
console.log(req.body);
cb(null, "./public/images/");
},
filename: (req, file, cb) => {
cb(null, req.body.name)
},
});
const upload = multer({ storage });
router.post("/upload_image", upload.single('file'), (req,res)=>{
console.log(req.body);
try {
res.status(200).json("file uploaded successfully")
} catch (error) {
console.log(error.message);
}
});
如果我在
中打印req.body
router.post("/upload_image", upload.single('file'), (req,res)=>{
console.log(req.body.name)
console.log(req.file)
......................
.......................
})
此处打印名称,req.file
也打印。但是,里面
const storage = multer.diskStorage({
destination: (req, file, cb) => {
...................
..............................
},
filename: (req, file, cb) => {
console.log(req.body.name); //undefined
cb(null, req.body.name)
},
});
名称变为未定义且应用程序崩溃。为什么 req 对象没有转到 diskStorage
函数。这里有什么问题?
Note that req.body might not have been fully populated yet. It depends on the order that the client transmits fields and files to the server.
如果您在前端的文件前附加名称,该名称应该在req.body.name
.
处可用
// Append name first before we send the file
// so we can access it in the backend
data.append('name', fileName);
data.append('file', file);
或者,您可以将文件名作为第三个参数直接发送到追加函数:
data.append('file', file, fileName);
然后您可以使用 req.file.originalname
在后端访问文件名。每当您在 Node.js.
中处理文件上传时,请参阅此免费 Request Parsing in Node.js Guide
我已经从客户那里发送了这个请求。
if(file){
const data = new FormData()
const fileName = Date.now() + file.name
data.append('file', file) // file comes from state while submitting
data.append('name', fileName)
try {
await axios.post('/upload_image', data)
} catch (error) {
console.log(error.message);
}
}
节点
const multer = require('multer')
// file upload
const storage = multer.diskStorage({
destination: (req, file, cb) => {
console.log(req.body);
cb(null, "./public/images/");
},
filename: (req, file, cb) => {
cb(null, req.body.name)
},
});
const upload = multer({ storage });
router.post("/upload_image", upload.single('file'), (req,res)=>{
console.log(req.body);
try {
res.status(200).json("file uploaded successfully")
} catch (error) {
console.log(error.message);
}
});
如果我在
中打印req.bodyrouter.post("/upload_image", upload.single('file'), (req,res)=>{
console.log(req.body.name)
console.log(req.file)
......................
.......................
})
此处打印名称,req.file
也打印。但是,里面
const storage = multer.diskStorage({
destination: (req, file, cb) => {
...................
..............................
},
filename: (req, file, cb) => {
console.log(req.body.name); //undefined
cb(null, req.body.name)
},
});
名称变为未定义且应用程序崩溃。为什么 req 对象没有转到 diskStorage
函数。这里有什么问题?
Note that req.body might not have been fully populated yet. It depends on the order that the client transmits fields and files to the server.
如果您在前端的文件前附加名称,该名称应该在req.body.name
.
// Append name first before we send the file
// so we can access it in the backend
data.append('name', fileName);
data.append('file', file);
或者,您可以将文件名作为第三个参数直接发送到追加函数:
data.append('file', file, fileName);
然后您可以使用 req.file.originalname
在后端访问文件名。每当您在 Node.js.