首页 > 解决方案 > 使用 Multer 从 Nodejs 中的多个输入字段上传多个文件

问题描述

我正在尝试通过这种形式使用 multer 中间件从 Nodejs 的客户端上传文件:

html表单:

<form action="/newStaff" method="post" class="form-group " enctype="multipart/form-data">

            <p>
            <label for="fName">First Name</label>
            <input type="text" id="fName" name="firstName" placeholder="Applicant's First Name" >
            </p>
            <p>
                <label for="oName">Other Name(s)</label>
                <input type="text" id="oName" name="otherName" placeholder="Applicant's Other Name(s)">
            </p>
            <p>
                <label for="lName">Last Name</label>
                <input type="text" id="lName" name="lastName" placeholder="Applicant's last Name" >
            </p>
            <p>
                <label for="gender">Gender</label>
                <input type="radio" id="gender" name="gender" value="Female" > Female
                <input type="radio" id="gender" name="gender" value="Male" > Male
            </p>
            <p>
                <label for="dob">Date of Birth</label>
                <input type="date" id="dob" name="dob" placeholder="date of birth"> 
            </p>
            <p>
                <label for="address">Home Address</label>
                <input type="text" id="address" name="homeAddress" placeholder="home address">
            </p>
            <p>
                <label for="contact">Phone Number</label>
                <input type="tel" id="contact" name="contact" placeholder="contact numbers">
            </p>
            <p>
                <label for="qualification">Highest Qualification:</label><br>
                <input type="text" id="qualification" name="qualification">
                 
            </p>
            <p>
                <label for="upCert">Certificates</label>
                <input type="file" id="upCert" name="certificate"   multiple>
            </p>
          
            <p>
                <label for="cv">Curriculum Vitae (CV)</label>
                <input type="file" id="cv" name="cv">
            </p>
            <p>
                <label for="photo">Photo</label>
                <input type="file" id="photo" name="p"  >
            </p>
            <p>
                <button>Submit</button>
            </p>
        </form>   

中间件是:

const multer = require('multer');
const upload = multer({dest: 'uploads/'});
const multiUpload = upload.fields([{certificate: 'certificate', 5}, {cv: 'cv', 1}, {photo: 'photo', 1}]);

邮寄路线是:

routes.post('/newStaff',multiUpload, cont.postNewStaffPage);

注意:我的后处理程序文件在路由文件夹中,而不是在 app.js 中。

到目前为止,我一直收到相同的错误消息:

MulterError: Unexpected field
    at wrappedFileFilter (C:\Users\USER\Documents\Developer Folders\Nodejs\Nodejs Practices\Practice-2 EJS\node_modules\multer\index.js:40:19)
    at Busboy.<anonymous> (C:\Users\USER\Documents\Developer Folders\Nodejs\Nodejs Practices\Practice-2 EJS\node_modules\multer\lib\make-middleware.js:114:7)
    at Busboy.emit (node:events:327:20)
    at Busboy.emit (C:\Users\USER\Documents\Developer Folders\Nodejs\Nodejs Practices\Practice-2 EJS\node_modules\busboy\lib\main.js:38:33)
    at PartStream.<anonymous> (C:\Users\USER\Documents\Developer Folders\Nodejs\Nodejs Practices\Practice-2 EJS\node_modules\busboy\lib\types\multipart.js:213:13)
    at PartStream.emit (node:events:327:20)
    at HeaderParser.<anonymous> (C:\Users\USER\Documents\Developer Folders\Nodejs\Nodejs Practices\Practice-2 EJS\node_modules\dicer\lib\Dicer.js:51:16)
    at HeaderParser.emit (node:events:327:20)
    at HeaderParser._finish (C:\Users\USER\Documents\Developer Folders\Nodejs\Nodejs Practices\Practice-2 EJS\node_modules\dicer\lib\HeaderParser.js:68:8)
    at SBMH.<anonymous> (C:\Users\USER\Documents\Developer Folders\Nodejs\Nodejs Practices\Practice-2 EJS\node_modules\dicer\lib\HeaderPar    at SBMH.push (C:\Users\USER\Documents\Developer Folders\Nodejs\Nodejs Practices\Practice-2 EJS\node_modules\streamsearch\lib\sbmh.js:56:14)
    at HeaderParser.push (C:\Users\USER\Documents\Developer Folders\Nodejs\Nodejs Practices\Practice-2 EJS\node_modules\dicer\lib\HeaderParser.js:46:19)
    at Dicer._oninfo (C:\Users\USER\Documents\Developer Folders\Nodejs\Nodejs Practices\Practice-2 EJS\node_modules\dicer\lib\Dicer.js:197:25)
    at SBMH.<anonymous> (C:\Users\USER\Documents\Developer Folders\Nodejs\Nodejs Practices\Practice-2 EJS\node_modules\dicer\lib\Dicer.js:127:10)

目前没有发送任何输入字段,即使没有文件类型输入也不起作用。

我要上传所有文件

标签: node.jsexpressejsmulter

解决方案


推荐阅读