nodeJs学习-08 multer中间件,解析post文件,上传文件
const express=require(‘express‘); const bodyParser=require(‘body-parser‘); const multer=require(‘multer‘); const fs=require(‘fs‘); const pathLib=require(‘path‘); var objMulter=multer({dest: ‘./www/upload/‘}); //multer 解析post文件 multipart/form-data var server=express(); //错误 //server.use(bodyParser.urlencoded({extended: false})); server.use(objMulter.any()); server.post(‘/‘, function (req, res){ //新文件名 //‘./www/upload/dfb33662df86c75cf4ea8197f9d419f9‘ + ‘.png‘ var newName=req.files[0].path+pathLib.parse(req.files[0].originalname).ext; fs.rename(req.files[0].path, newName, function (err){ if(err) res.send(‘上传失败‘); else res.send(‘成功‘); }); //1.获取原始文件扩展名 //2.重命名临时文件 }); server.listen(8080);
相关推荐
webgm 2020-08-16
yezitoo 2020-06-06
始作俑者 2020-01-19
kaikai 2018-11-28
lei0 2020-11-02
igogo00 2020-07-04
iconhot 2020-06-26
88481456 2020-06-18
疯狂紫萧 2020-06-16
SelinaChan 2020-05-15
luckymaoyy 2020-04-25
80437700 2020-04-17
austindev 2020-04-07
我有一只小松鼠 2020-02-29
webfullStack 2020-02-15
binglingnew 2012-10-03
jinxiutong 2020-01-04
Dawnworld 2019-12-23