当前位置:网站首页>File expert ---multer
File expert ---multer
2022-06-22 09:20:00 【Jdoit CW】
Content
master multer Usage of modules
multer
A very easy to use file upload module , Official address :https://www.npmjs.com/package/multer
meanwhile formidable The module is also easy to use , Recommended reading :formidable Use
1. usage
Here is a summary of common usage in the project :
- First installation
multermodular
npm install --save multer
- Import this module and perform basic configuration
const multer = require('multer');
const upload = multer({
dest: './public/uploads' }).single('file'); // Upload a single file
app.post('/multertest', upload, (req, res) => {
res.send(req.file)
})
Be careful : Here uploads The path is relative to the server
- Test interface ( Take the picture )
①: use Postman To test

②: Simulate submission with a real form
Form code :
Be sure to write... Here :enctype="multipart/form-data"
<form action="/multertest" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value=" Submit ">
</form>
Submit results :
- The server
uploadsFolder

summary : You can see uploads The pictures under the folder have no suffix , If stored in the database , Low controllability , So we need to be right file Uploaded files To deal with
2. Information processing
In the actual project , We often want to upload pictures to Path to the server , And then put The path is stored in the database , When used , Request picture path , write in src attribute .
①:node Example :
const multer = require('multer');
const fs = require('fs');
const upload = multer({
dest: './public/uploads' }).single('file');
app.post('/multertest', upload, (req, res) => {
let file = req.file;
// fs.renameSync(oldname,newname); This method is used to change the file name ( Of documents oldname And newname Both in file In the object )
fs.renameSync('./public/uploads/' + file.filename, './public/uploads/' + file.originalname);
let imgurl = 'http://localhost:3000/uploads/' + file.originalname;
res.send(imgurl)
})
②:Postman test

③: The server uploads Folder

At this point, whether it is stored in the database , Or direct access , All become simple and direct .
3. Multiple file upload
Multi file uploads are similar to single file uploads , It's just When creating path parameters , Limit it , The demonstration is brief , Go straight to the example
Example :
const multer = require('multer');
const fs = require('fs');
let uploadMore = multer({
dest: './public/uploads' }).array('file', 5);// Here to 5 For example ( Number of uploads <= 5)
app.post('/multertest', uploadMore , (req, res) => {
let files = req.files;
for (var k in files) {
let file = files[k];
fs.renameSync('./public/uploads/' + file.filename, './public/uploads/' + file.originalname);
let url = 'http:localhost:3000/uploads' + file.originalname;
}
})
over
边栏推荐
- Sound and shadow 2022 heavy release! Detailed explanation of the new functions of Huisheng Huiying 2022
- Performance optimization topics
- Yiwen approaches ZMQ
- container_ of
- MySQL field attribute list sends a document for future reference
- Brush questions in C language | output 6 decimal places with%f control character (17)
- 让你轻松上手【uni-app】
- Alibaba big fish SMS interface PHP version, simplified version Alibaba big fish SMS sending interface PHP instance
- rewrite? Reload? Are you dizzy?
- . A use of file link library
猜你喜欢
![[tensorboard] step on all minefields and solve all your problems](/img/35/fc0f7ed311bf7c0321e1257ff6a1a6.png)
[tensorboard] step on all minefields and solve all your problems

Jinglianwen Technology: product summary of machine learning AI dataset (III)

Sound and shadow 2022 heavy release! Detailed explanation of the new functions of Huisheng Huiying 2022

kali木马入侵win7系统

DOM编程
![[network security officer] an attack technology that needs to be understood - high hidden and high persistent threats](/img/c9/c0ee95e816cac698f5397cc369d9ec.jpg)
[network security officer] an attack technology that needs to be understood - high hidden and high persistent threats

文件小能手---multer

【node】快收下爬虫,我们不再为数据发愁

嵌入式开发专业术语概念汇总

前馈和反向传播
随机推荐
景联文科技:机器学习AI数据集产品汇总(三)
User insight into the video industry in January 2022: active users began to pick up under the influence of holidays
Didi's two-sided summary
文件小能手---multer
Opencv daily function histogram correlation (3)
Express bird of Express query demonstration code (php+curl)
list_head
集合中的类--->你关注过那些是线程安全的吗?
求余弦的大小
Record some Oracle operation commands
Alibaba big fish SMS interface PHP version, simplified version Alibaba big fish SMS sending interface PHP instance
值(址)传递,看清名字,别掉沟里
File upload attack and protection
Php+sql get the field name and detailed attributes of MySQL data table
Lifecycle of Servlet
mknod
C language brush question | temperature conversion (11)
看看volatile你深知多少
PHP seven methods to obtain complete instances of file name suffixes [collect]
IS_ERR()