【问题标题】:How to ignore uploading a image file by using Nodejs and multer如何使用 Nodejs 和 multer 忽略上传图像文件
【发布时间】:2023-01-07 02:42:24
【问题描述】:

我是 nodejs 的初学者,我正在使用 multer 上传图像文件。我希望用户能够提交一个表单,其中发布图片是可选的 但是,如果我提交并跳过图像文件的选择,则会发生错误:“无法读取未定义的属性(读取“目标”)”。 谁能告诉我当用户提交没有选择图像文件的表单时如何处理。

这是代码:

const multer = require('multer');
const path = require('path');


const storage = multer.diskStorage({
destination: (req,file,cb) => {
    cb(null,'public/images/words');
},
filename:(req,file,cb)=>{
    console.log(file);
    cb(null, Date.now() + path.extname(file.originalname));
}
});

const upload = multer({storage:storage});

module.exports = upload;

enter image description here

【问题讨论】:

  • 你能展示错误所指的代码吗? submitWord 函数?
  • 我非常感谢,因为你发表了评论,但我已经解决了这个问题。错误在 submitWord 中。总之非常感谢。
  • 好的,请记住始终阅读您的堆栈跟踪。

标签: node.js multer image-file


【解决方案1】:

你能解释一下你是怎么得到答案的吗?

【讨论】:

猜你喜欢
  • 2021-11-23
  • 1970-01-01
  • 2021-09-21
  • 1970-01-01
  • 2018-07-21
  • 2019-04-30
  • 2019-12-05
  • 2017-03-29
  • 1970-01-01
相关资源
最近更新 更多