当前位置:网站首页>Nodejs uses Multe to upload files and store them on the server
Nodejs uses Multe to upload files and store them on the server
2022-07-16 07:59:00 【Little Ali】
install multer
yarn add multer or npm install multer
In need of use multer Import from the file
const multer = require("multer")
Interface for writing and uploading files
// adopt req.file Receive the information of the transmitted file
/* "file": { "fieldname": "file", "originalname": "5fbc140c-f2c6-490d-a9f8-e0a3c648fc46.jpg", // file name "encoding": "7bit", // code "mimetype": "image/jpeg", // file type "destination": "E:\\project\\works\\works_server\\public", // Storage path "filename": "909660f11ecbfe82494dc79aac9e60dc", "path": "E:\\project\\works\\works_server\\public\\909660f11ecbfe82494dc79aac9e60dc", // The absolute path to the file "size": 1400503 // file size } */
router.post("/api/upload",multer({
dest:path.join(__dirname,"../public")
}).single("file"),
(req,res)=>{
// Get the upload file file name 、 Absolute path
const {
originalname} = req.file
const paths = req.file.path
const extname = path.extname(originalname) // Get the extension of the file
let name = Date.now() + extname // New name
let newPath = path.join(__dirname,"../public/") + name // New path
fs.rename(paths,newPath,err=>{
// adopt fs Modular rename Method Modify the file name again
if (err) return res.send({
message:' Upload error ',status:400})
// Here, the file name can be stored in the database field for easy front-end access
res.send({
message:' Upload successful ',
name,
newPath,
file:req.file
})
})
})
边栏推荐
- .net 5使用LogDashboard
- How to apply @transactional transaction annotation to perfection?
- 1. Stm32f4 USB protocol research - USB protocol
- C# .Net Core and Net5 Skills record
- CentOS 7x installing MySQL database
- POI framework learning - Import and export cases
- C# excel net core读取xlsm
- Number system conversion and subnet Division
- Day 10 of leetcode
- 64位整除乘法
猜你喜欢

C#使用TcpListener进行聊天通信

Are you still using the strategy mode to solve if else? Map+ functional interface method is yyds

Day 8 of leetcode question brushing

4年测试工程师经历,下一步转开发还是继续测试?

C#驗證碼

POI框架学习-导入导出案例

In 2 years, the salary increased by 20K, and the transformation from outsourcing manual to test manager

Day 12 of leetcode + day 1 of DL

File management - Alibaba cloud OSS learning (I)

Redis只能做缓存?太out了!
随机推荐
Day 9 of leetcode question brushing
Principle and configuration of static routing
Why can't we use redis expiration monitoring to close orders?
HCIA review
2021-07-02
Republish experiment
The experience of finding a job after coming out of the software testing training class taught me these five things
程序猿专属“压测工具”并发模拟
Druid database connection pool monitoring page
Dynamic open point segment tree
C# . Net Core and Net5 Skills record
c#使用MQTT通信
prism导航功能
XPath超详细总结
1. STM32F4 USB协议研究 - USB协议
Network layer protocol
Day 11 of leetcode question brushing
ABAP Bapi copy the standard project template to achieve project initiation
还在使用 MySQL 中使用枚举?这些陷阱一定要注意!
Heap and heap sort