In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "what is the concept of multer in node". In daily operation, I believe that many people have doubts about the concept of multer in node. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what is the concept of multer in node?" Next, please follow the editor to study!
In node, multer is a middleware used to handle the "multipart/form-data" type data format, mainly for uploading files; after parsing the request body, the middleware adds a body object and a file or files object to the Request object.
This article operating environment: Windows10 system, nodejs version 12.19.0, Dell G3 computer.
What does multer mean in node
Multer is a node.js middleware used to process form data of type multipart/form-data, which is mainly used for uploading files. It is written on busboy and is very efficient.
Note: Multer does not process any non-multipart/form-data type form data
After parsing the request body, Multer adds a body object and a file or files object to the Request object (using the files object when uploading multiple files).
Where the body object contains the text fields (if any) from the submitted form, and the file (or files) object contains files uploaded through the form.
Tips:multipart/form-data is used to specify the special type of data to be transmitted, mainly the non-text content we upload, such as pictures or mp3, etc.
Const express = require ('express') const multer = require (' multer') const app = express () const storage = multer.diskStorage ({/ / Save path destination: function (req, file, cb) {cb (null,'/ tmp/my-uploads') / / Note the file path here, not the relative path Just fill in the file name filename: function (req, file, cb) {cb (null, file.fieldname +'-'+ Date.now ()}) const upload = multer ({storage: storage}) app.post ('/ profile', upload.single ('avatar'), function (req, res) Next) {/ / req.file is the information of the `avatar` file / / req.body will have text field data If any}) app.post ('/ photos/upload', upload.array ('photos', 12), function (req, res, next) {/ / req.files is the information of the `photos` file array / / req.body will have text field data If any}) const cpUpload = upload.fields ([{name: 'avatar', maxCount: 1}, {name:' gallery', maxCount: 8}]) app.post ('/ cool-profile', cpUpload, function (req, res, next) {/ / req.files is an object (String-> Array) key is the file name The value is an array of files / / for example: / / req.files ['avatar'] [0]-> File / / req.files [' gallery']-> Array / / req.body will have text field data, if any})
Multer (options)
Multer accepts an options object, the most basic of which is the dest attribute, which tells Multer where to save the uploaded file. If you omit the options object, these files will be saved in memory and will never be written to disk.
In general, a typical web application only needs to set the dest property, like this:
Const upload = multer ({dest: 'uploads/'})
If you want more control when uploading, you can use the storage option instead of dest. Multer has two storage engines, DiskStorage and MemoryStorage; in addition, more engines are available from third parties.
At this point, the study of "what is the concept of multer in node" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.