【问题标题】:express fileupload package problem with the pathexpress fileupload 包的路径问题
【发布时间】:2021-10-12 05:57:36
【问题描述】:

我想将文件上传到“公共”文件夹时遇到问题。当我不使用'./' 时会出现此问题。如果我使用'../''/' 文件不要上传到公用文件夹。

**controllers.js**
files.forEach(file => {
        file.mv('../public/' + file.name, err => {
            if (err) {
                log({ err });
                return res.redirect('/')
            }

            log('file uploaded');

        })
    })

错误:Error: ENOENT: no such file or directory, open 'D:\Users\Antonio\Desktop\public\image4.jpg'

还有这个错误:Express-file-upload: Request is not eligible for file upload!

当我在 controllers/controllers.js 里面时会发生这种情况。如果我在app.js 中编写此代码,我不会遇到任何问题。

【问题讨论】:

    标签: node.js image express upload express-fileupload


    【解决方案1】:

    file.mv() 方法相对于cwd 当前工作目录路径起作用。

    要让它工作,你需要这样写:

    const path = require('path')
    
    const fileName = path.join(__dirname, '../public/', file.name)
    
    file.mv(fileName)
    

    这样做,路径将相对于controller.js 文件(而不是相对于cwd

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-09
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 2012-05-05
      相关资源
      最近更新 更多