In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.