首页 > 解决方案 > Express & Multer 不上传文件单个/任何导致 req.file/req.files 未定义或 []

问题描述

这个问题把我难住了。我的应用程序需要在平台上上传包含要添加/修改等内容的 excel 文件。为此,我需要 Multer 上传 excel 文件,并将其存储在其中以os.tempdir()供阅读。

问题:

使用建议的方法和替代方法,我无法通过浏览器将文件上传到我的应用程序(在本地运行)。我尝试过使用.single("input file name here")将文件放入 .single("input file name here") 的方法req.file,或者将文件放入 .single() 的any()方法req.files。这些都不会返回成功的文件上传,因此req.file永远是undefined并且req.files永远是[]

我有什么代码?

您将在下面找到我的网页和服务器端代码:

网页(玉)

...
form(action=`/companies/${companyId}/employees/upload` enctype="multipart/form-data" method="post")
    input(type="file", accept=".xlsx,.xls" , name="uploadEmployees", style="background: transparent" required)
    input(type="hidden" name="test" value="test")
    button#submitUpload.btn.btn-primary(type='submit') Save changes
...

笔记:

服务器端代码:

const multer = require('multer')
const uploadSpreadsheet = multer().any();

// router prefix is /companies
router.post('/:id/employees/upload', (req, res, next) => {
    uploadSpreadsheet(req, res, (err) => {
        if (err instanceof multer.MulterError) {
            console.log("Multer error")
            console.error(err);
        } else if (err) {
            console.log("Another error")
            console.log(err)
        } else {
            console.log("Multer function success, next()")
            next()
        }
    });
}, async (req, res, next) => {
    console.log("Using multer.any()")
    console.log("req.files should not be empty")
    console.log(`${req.files ? (req.files.length > 0 ? "req.files has some files" : "req.files has no files") : "req.files is undefined"}`)
    console.log(`We should expect to find a hidden field named 'test' with value 'test'`)
    console.log(`${req.body === {} ? "req.body is empty" : req.body["test"] ? "hidden field found" : "no hidden field named 'test' found"}`)
    console.log(req.body);
    if (!req.file || req.file === "") {
        req.flash("message", [{
            status: false,
            message: "No file uploaded or upload failed"
        }]);
        return res.redirect(`/companies/${req.params.id}/employees`)
    }

    // read the entire file
    const workbook = new ExcelJS.Workbook();
    await workbook.xlsx.readFile(req.file);
    ...

笔记:

控制台输出:

i  functions: Beginning execution of "us-central1-app"
>  Multer function success, next()
>  Using multer.any()
>  req.files should not be empty
>  req.files has no files
>  We should expect to find a hidden field named 'test' with value 'test'
>  no hidden field named 'test' found
>  [Object: null prototype] {}

由于该行出现,人们应该期望在或Multer function success, next()中找到文件,但这不会发生。req.filesreq.file

任何解决此问题的帮助将不胜感激!


Multer 单文件上传(为了完整起见)

我在下面定义了我的单个文件上传功能和参数:

const os = require('os')
const tmp = os.tmpdir();

function fileFilter(req, file, cb) {
    // for testing
    cb(null, true);
}

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, `${tmp}`)
    },
    filename: function (req, file, cb) {
        cb(null, v4())
    }
})
// const uploadSpreadsheet = multer({storage: storage, fileFilter: fileFilter}).single("uploadEmployees");

在发布路线中,这是使用我的单个文件上传时的样子:

router.post('/:id/employees/upload', uploadSpreadsheet, async (req, res, next) => {
    ...

通过上述这些服务器端代码更改,客户端代码保持不变

标签: javascriptnode.jsexpressuploadmulter

解决方案


推荐阅读