Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use node+multer middleware to upload single file and multiple files

Shulou Source: shulou.com Published: 2022-06-01 04:48:26 10月12日 Update

This article introduces the relevant knowledge of "how to use node+multer middleware to achieve single-file, multi-file upload". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. concept

Multer is a node.js middleware used to process form data of type multipart/form-data, which is mainly used for uploading files.

Note: Multer does not process any non-multipart/form-data type form data.

II. Installation

Npm i multer-save

Third, use (there are single file and multi-file upload cases at the bottom of the page)

Multer adds a body object (containing the text field information of the form) and a file or files object to the request object of the express (single file is obtained through req.file, multiple files are obtained through req.files, and file or files object contains the file information uploaded by the object form).

IV. Basic use

/ / 1. Introduce dependency

Const express = require ('express')

Const multer = require ('multer')

/ / it is stored under the uploads folder and is not created directly

Const upload = multer ({dest: 'uploads/'})

Const app = express ()

/ / same

/ / the following fielname (string) must have the same property name of the myformData object passed from the front end, and the file object / / req.body will have text field data if it exists.

App.post ('/ profile', upload.single (fielname), function (req, res, next) {

/ / multer instance .single (fielname)-A single file is uploaded, and the file object is obtained through req.file

/ / multer instance .single (fieldame)-accepts a file named fielname whose information is stored in the fielname attribute in req.file

/ / req.body will have text field data, if any

})

App.post ('/ photos/upload', upload.array (fielname, 12), function (req, res, next) {

/ / multer instance .array (fielname [, maxCount])-receives an array of files named fielname; maxCount-- limits the maximum number of files to be uploaded, and the information of these files is stored in req.files

})

Const cpUpload = upload.fields ([{name:fields [, maxCount: 1]}, {name: 'gallery', maxCount: 8}])

/ / multer instance .files (fields)-accept the mixed file of the specified fields and get-- req.files

/ / fields should be an array of objects with name and optional maxCount attributes

App.post ('/ profile', upload.none (), function (req, res, next) {

/ / multer instance .none ()-only text fields are accepted

})

"how to use node+multer middleware to achieve single-file, multi-file upload" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Files objects instances information data forms fields middleware properties content arrays more cases knowledge types processing practical maximum same consistent Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Redmi vpn MariaDB Shulou Information