nodeJs学习-08 multer中间件,解析post文件,上传文件

fm00 2019-11-01

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);

相关推荐