【问题标题】:Multer is grabbing images but it is not saving my files in the correct directoryMulter 正在抓取图像,但它没有将我的文件保存在正确的目录中
【发布时间】:2022-01-28 05:13:26
【问题描述】:

我有一个快递服务器。当用户去对 profiel 发出 post 请求时。他们提交个人资料图片。我正在尝试使用 multer 来获取正在发送的照片/图像并将其存储在后端目录的文件夹中。

我设置了 multer,但由于某种原因,它没有保存任何我希望它保存在本地以便我可以检索它的照片。我该如何解决这个问题。有没有人知道如何解决这个问题。

控制器:

const Profile = require("../../models/UserModels/Profiles.js")
const User = require('../../models/UserModels/Users.js')

const multer = require('multer')
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '../../client/images/profile/')
  },
  filename: function (req, file, cb) {
    const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
    cb(null, file.fieldname + '-' + uniqueSuffix)
  }
})
const upload = multer({ storage: storage })
...
  post:(req, res) => {
    console.log(req.body)
    console.log(req.files)
    upload.single(req.files.profile_pic.name) <----------------
    let body = req.body
    Profile.create({
      profile_pic: req.files.profile_pic.name,
      f_name: body.f_name,
      l_name: body.l_name,
      bio: body.bio,
      location: body.location,
      sm_facebook: body.sm_facebook,
      sm_instagram: body.sm_instagram,
      sm_twitter: body.sm_twitter,
      sm_website: body.sm_website,
      followers: body.followers,
      following: body.following,
      photos: body.photos,
      downloads: body.downloads,
      edits: body.edits,
      userId: body.userId
    })
    .then((data) => {
      res.send(data).status(200)
    })
    .catch((err) => {
      console.error(err)
      res.send(err).status(400)
    })
  },

这是此后端的目录:

-----------更新---------------

这是来自前端的 react axios 请求:

function createProfile(userId){
    let data = new FormData()
    data.append('f_name', firstName)
    data.append('l_name', lastName)
    data.append('bio', bio)
    data.append('location', location)
    data.append('sm_facebook', facebook)
    data.append('sm_instagram', instagram)
    data.append('sm_twitter', twitter)
    data.append('sm_website', website)
    data.append('followers', 0)
    data.append('following', 0)
    data.append('photos', 0)
    data.append('edits', 0)
    data.append('downloads', 0)
    data.append('userId', userId)
    data.append('profile_pic', profilePicture)
    console.log(data)
    axios({
      method: "post",
      url: "/api/profile/",
      data: data,
      headers: { "Content-Type": "multipart/form-data" },
    })
    .then((data) => {
      return(<Navigate to='/' />)
    })
    .catch((err) => {
      console.error(err)
    })
  }

【问题讨论】:

  • 好的,根据您提供的 axios 示例,您最终会得到 router('base/route').post('example', upload.single('profile_pic'), (req, res) =&gt; { .. })。在此示例中,base/route 是您的 express 路由器的主路由,example 是您的控制器的特定路由。正如我之前告诉你的,upload.single() 应该用作中间件,在你的情况下,它应该与我提供的示例相匹配。

标签: javascript node.js express multer


【解决方案1】:

您必须指定 multer 的存储路径,就好像您位于项目的根文件夹中一样。 所以不要使用'../../client/images/profile/'作为存储路径,你应该使用:'./backend/client/images/profile'

你的代码最终应该如下:

const multer = require('multer')
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './backend/client/images/profile')
  },
  filename: function (req, file, cb) {
    const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
    cb(null, file.fieldname + '-' + uniqueSuffix)
  }
})

【讨论】:

  • 感谢您的帮助。快速提问,当我调用上传时,我是否必须传入图像名称或图像对象。我都尝试了,但最终没有将其存储在配置文件目录中。
  • 另外,我正在调用上传,但似乎没有调用任何内容。我在filename: 中添加了console.log(),它没有被显示,这让我相信它没有被调用。你知道为什么会这样吗
  • 您应该使用upload.single() 函数作为中间件。此外,single() 采用您提供文件的字段的名称。例如:如果包含照片的字段名称是“图片”,您最终会得到类似upload.single('picture') 的内容。然后在实际控制器上,您将可以访问 req 对象 (req.file) 上的 file 属性;它将包含所提供文件的信息。
  • 我尝试了它的文件名,但它没有做任何事情。当您说upload.single() 作为中间件时。它不应该在控制器中。如果它不属于控制器,我应该在哪里调用upload.single()。我觉得我快要得到它了,但它没有保存..
  • 你应该在你的控制器路由之后传递upload.single()(同样,它是一个中间件)。例如:.post('example', upload.single('field_name_on_the_form_data'), (req, res) =&gt; { .. }) 此外,您应该传递给upload.single() 的名称是包含文件的字段的名称。如果您在form data 中提供了文件,并且包含该文件的字段名为fileimage,那么您将作为参数传递给upload.single(),您最终会得到:upload.single('file'),例如例子。
猜你喜欢
  • 2022-01-07
  • 2023-01-12
  • 1970-01-01
  • 2021-03-05
  • 2021-07-01
  • 1970-01-01
  • 2016-08-15
  • 2020-07-31
  • 2018-11-16
相关资源
最近更新 更多