Node—— 使用express + multer 实现文件上传功能

const express = require('express')
const multer = require('multer')
const router = express.Router()

var storage = multer.diskStorage({
    //设置文件存储路径
    destination: function (req, file, cb) {
        cb(null, 'D:/JAVA/nz-device/static/img')
    },
    //设置文件名
    filename: function (req, file, cb) {
        var fileFormat = (file.originalname).split(".");
        cb(null, file.fieldname + '-' + Date.now() + "." + fileFormat[fileFormat.length - 1]);
    }
})

var upload = multer({ storage: storage }).array("file",2)

router.post("/upload",upload,(req,res,next)=>{
    console.log(req.files);
    res.json("ok");
})

你可能感兴趣的:(Node,nodejs)