nodejs express 上传下载
上传模块 npm i multer or yarn multer
引入
ar multer = require('multer');
var fs = require('fs');
const UPLOAD_PATH = './uploads'
var upload = multer({
dest: UPLOAD_PATH
})
router.post('/upload', upload.array('fileUpload'), function(req, res, next) { const files = req.files; const response = []; const result = new Promise((resolve, reject) => { files.map((v) => { fs.readFile(v.path, function(err, data) { fs.writeFile(`${UPLOAD_PATH}/${v.originalname}`, data, function(err, data) { const result = { file: v, } if (err) reject(err); resolve('成功'); }) }) }) }) result.then(r => { res.json({ msg: '上传成功', }) }).catch(err => { res.json({ err }) }); })
下载
//下载 router.get('/download', function(req, res, next) { res.download('uploads/' + q.name); });