1. 程式人生 > 其它 >Express檔案上傳

Express檔案上傳

路由攔截

var express = require('express');
var router = express.Router();
var user = require('../controllers/userController')
// 檔案上傳-中介軟體multer
let multer = require('multer')
// 設定檔案上傳後的儲存地址
let upload = multer({ dest: './public/images/' })

// 單檔案上傳
router.post('/uploadUserHeadImg', upload.single('file'), user.uploadUserHeadImg);
// 多檔案上傳
router.post('/uploadImgs', upload.array('files', 5), user.uploadImgs);
 
module.exports = router;

處理函式
其實在進入該業務邏輯處理函式之前,也就是路由攔截部分,檔案已經上傳成功,此處的作用就是處理業務上的邏輯了,比如資料庫操作等

var dbConfig = require('../util/dbConfig')

// 檔案上傳
let fs = require('fs') // 引入檔案處理物件
const uploadUserHeadImg = (req, res) => {
    if (!req.file) {
        res.send({
            code: 400,
            msg: '上傳檔案不能為空'
        })
    } else {
        let file = req.file
        // 修改檔名字
        fs.renameSync('./public/images/' + file.filename, './public/images/' + file.originalname)
        // 設定響應型別以及編碼
        res.set({
            'cpntent-type': 'application/json; charset=utf-8'
        })
        let { id } = req.query
        let imageUrl = 'http://localhost:3006/images/' + file.originalname
        let sql = 'update user set pic=? where id=?'
        let sqlArr = [imageUrl, id]
        let callBack = (err, data) => {
            if (err) {
                res.send({
                    code: 400,
                    msg: '上傳成功,插入失敗'
                })
            } else {
                if (data.affectedRows === 1) {
                    res.send({
                        code: 200,
                        msg: '上傳成功',
                        filePath: imageUrl
                    })
                } else {
                    res.send({
                        code: 400,
                        msg: '資料庫插入失敗'
                    })
                }
            }
        }
        dbConfig.sqlConnect(sql, sqlArr, callBack)
    }
}
// 多檔案上傳
const uploadImgs = (req, res) => {
    let files = req.files
    if (!files.length) {
        res.send({
            code: 400,
            msg: '上傳檔案不能為空'
        })
    } else {
        for (let file of files) {
            // 設定響應型別以及編碼
            res.set({
                'cpntent-type': 'application/json; charset=utf-8'
            })
            // 修改檔名字
            fs.renameSync('./public/images/' + file.filename, './public/images/' + file.originalname)
            let imageUrl = 'http://localhost:3006/images/' + file.originalname
            let sql = 'insert into attach_file (img_path, create_at) values (?,?)'
            let sqlArr = [imageUrl, new Date()]
            let callBack = (err, data) => {
                if (err) {
                    res.send({
                        code: 400,
                        msg: '上傳成功,插入失敗'
                    })
                } else {
                    if (data.affectedRows === 1) {
                        res.send({
                            code: 200,
                            msg: '上傳成功',
                            filePath: imageUrl
                        })
                    } else {
                        res.send({
                            code: 400,
                            msg: '資料庫插入失敗'
                        })
                    }
                }
            }
            dbConfig.sqlConnect(sql, sqlArr, callBack)
        }
    }
}

module.exports = {
    uploadUserHeadImg,
    uploadImgs
}