首页 > 解决方案 > 在 nodejs 和 vuejs 的文件夹中获取我保存的图像名称时出错

问题描述

我使用 multer 中间件上传了我的图片

const storage = multer.diskStorage({
      destination: function(req, file, cb) {
        // fs.mkdir('../uploads/', (err) => {
          cb(null, './public/images/')
          // console.log(err)
        
      },

      filename: function(req, file, cb) {
        cb(null, `${Date.now()}-hemmyhtec-${file.originalname}`)
      }
    })
    const fileFilter = (req, file, cb) => {
      if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
        cb(null, true)
      } else {
        cb(null, false)
        const error = new Error('Wrong File, only image are allowed')
        console.log(error)
      }
    }

我的图像已保存到文件夹中,并将名称作为文件保存到数据库中:(“1632760261507-hemmyhtec-1.png”)。

我保存的信息和文件名的日志

因此,当我在数据库中获取数据和文件名时,我在下面得到了这个 在此处输入图像描述

It added "\" at the beginning \"" and also at the end of the file name.

在获取我的 vuejs 代码时,这给了我一个错误

在此处输入图像描述

谁能告诉我问题或更好的方法?

创建文件的代码...

  const file = req.file; 

   MentorProfile.create({
    file: file.filename,
    profession: req.body.profession,
    about: req.body.about.....

标签: javascriptnode.jsvue.jsmulter

解决方案


推荐阅读