试图将图像上传到 nodejs api 但它 returns 出错
trying to upload image to nodejs api but it returns with error
我正在尝试将图片上传到我的 node.js 服务器,当我什么都不上传,只是将请求发送到 post 而没有图片时,它工作正常,但每当我尝试 post 图像 node.js 在控制台中显示错误
这里是 link 到 post
“http://localhost:3001/databaseGym/logo”
这是我的代码来处理 post 对 link
的操作
Router.post('/logo', upload.single('file'), (req, res) => {
console.log("working");
//res.json({ file: req.file });
res.send("done");
});
这是我在 upload.single()
中上传的代码
// Create storage engine
const storage = new GridFsStorage({
url: url,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err);
}
const filename = buf.toString('hex') + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'logo'
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
这里是 postman 应用程序的屏幕截图,我通过它发送图像
这里是错误
MulterError: Unexpected field
at wrappedFileFilter (D:\web dev\gym website\server_gym\node_modules\multer\index.js:40:19)
at Busboy.<anonymous> (D:\web dev\gym website\server_gym\node_modules\multer\lib\make-middleware.js:114:7)
at Busboy.emit (node:events:394:28)
at Busboy.emit (D:\web dev\gym website\server_gym\node_modules\busboy\lib\main.js:38:33)
at PartStream.<anonymous> (D:\web dev\gym website\server_gym\node_modules\busboy\lib\types\multipart.js:213:13)
at PartStream.emit (node:events:394:28)
at HeaderParser.<anonymous> (D:\web dev\gym website\server_gym\node_modules\dicer\lib\Dicer.js:51:16)
at HeaderParser.emit (node:events:394:28)
at HeaderParser._finish (D:\web dev\gym website\server_gym\node_modules\dicer\lib\HeaderParser.js:68:8)
at SBMH.<anonymous> (D:\web dev\gym website\server_gym\node_modules\dicer\lib\HeaderParser.js:40:12)
您应该将 Postman 中的参数从 image
更改为 file
,因为您在 POST 端点中指定了 属性 名称:
// you specify the `file` as the fieldname
Router.post('/logo', upload.single('file'), (req, res) => {
console.log("working");
//res.json({ file: req.file });
res.send("done");
});
来自文档:
.single(fieldname)
:
Accept a single file with the name fieldname
. The single file will be stored in req.file
.
我正在尝试将图片上传到我的 node.js 服务器,当我什么都不上传,只是将请求发送到 post 而没有图片时,它工作正常,但每当我尝试 post 图像 node.js 在控制台中显示错误
这里是 link 到 post “http://localhost:3001/databaseGym/logo” 这是我的代码来处理 post 对 link
的操作 Router.post('/logo', upload.single('file'), (req, res) => {
console.log("working");
//res.json({ file: req.file });
res.send("done");
});
这是我在 upload.single()
中上传的代码// Create storage engine
const storage = new GridFsStorage({
url: url,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err);
}
const filename = buf.toString('hex') + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'logo'
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
这里是 postman 应用程序的屏幕截图,我通过它发送图像
这里是错误
MulterError: Unexpected field
at wrappedFileFilter (D:\web dev\gym website\server_gym\node_modules\multer\index.js:40:19)
at Busboy.<anonymous> (D:\web dev\gym website\server_gym\node_modules\multer\lib\make-middleware.js:114:7)
at Busboy.emit (node:events:394:28)
at Busboy.emit (D:\web dev\gym website\server_gym\node_modules\busboy\lib\main.js:38:33)
at PartStream.<anonymous> (D:\web dev\gym website\server_gym\node_modules\busboy\lib\types\multipart.js:213:13)
at PartStream.emit (node:events:394:28)
at HeaderParser.<anonymous> (D:\web dev\gym website\server_gym\node_modules\dicer\lib\Dicer.js:51:16)
at HeaderParser.emit (node:events:394:28)
at HeaderParser._finish (D:\web dev\gym website\server_gym\node_modules\dicer\lib\HeaderParser.js:68:8)
at SBMH.<anonymous> (D:\web dev\gym website\server_gym\node_modules\dicer\lib\HeaderParser.js:40:12)
您应该将 Postman 中的参数从 image
更改为 file
,因为您在 POST 端点中指定了 属性 名称:
// you specify the `file` as the fieldname
Router.post('/logo', upload.single('file'), (req, res) => {
console.log("working");
//res.json({ file: req.file });
res.send("done");
});
来自文档:
.single(fieldname)
:Accept a single file with the name
fieldname
. The single file will be stored inreq.file
.