@[TOC] 实现视频和图片的上传
个人学习笔记
实现代码如下:
-
我的文件目录结构 -
路由 router upload.js
const express = require('express')
const router = express.Router()
const multer = require('multer')
const path = require('path')
const uploads = multer({dest: path.join(__dirname, '../temp_upload')})
const uploadHandle = require('../router_handle/upload')
router.post('/upload', uploads.array('upload',12),uploadHandle.upload)
module.exports = router
- 处理函数模块 router_handle upload.js
const fs = require('fs')
const moment = require('moment')
const path = require('path')
const db = require('../db/config')
exports.upload = (req, res) => {
const info = req.body
const files = req.files
var fileList = []
let fileType = ["avi", "wmv", "mpg", "mpeg", "mov", "rm", "ram", "swf", "flv", "mp4", "mp3", "wma", "avi", "rm", "rmvb", "flv", "mpg", "mkv"]
let imgType = ['jpg','png','jpeg','gif','webp','bmp','tif']
for (item in files) {
var file = files[item]
var type = file.originalname.split('.')[1]
var ttt = moment().format("YYYYMMDDHHmmss")
var ran = parseInt(Math.random() * 89999 + 10000)
var newName = ttt + ran
if (fileType.indexOf(type) !== -1) {
var newpath = path.join(process.cwd(), "/upload/video/" + newName + "." + type)
fs.renameSync(file.path, newpath)
} else if (imgType.indexOf(type) !== -1) {
var newpath = path.join(process.cwd(), "/upload/images/" + newName + "." + type)
fs.renameSync(file.path, newpath)
} else {
return res.cc('上传文件格式不正确')
}
file.path = newName
fileList.push(newpath)
}
if (fileType.indexOf(type) !== -1) {
const videoSql = `select video from patient where id = ?`
db.query(videoSql, info.id, (err, result) => {
if (err) return res.cc(err)
if (result.length !== 1) return res.cc('查询失败')
const vInserSql = `update patient set video = ?`
if (!result[0].video) {
db.query(vInserSql, JSON.stringify(fileList), (err, result) => {
if (err) return res.cc(err)
return res.cc('视频上传成功')
})
} else if (JSON.parse(result[0].video).length > 0) {
const sqlData = JSON.parse(result[0].video)
for (let i = 0; i < fileList.length; i++) {
let item = fileList[i]
sqlData.unshift(item)
}
db.query(vInserSql, JSON.stringify(sqlData), (err, result) => {
if (err) return res.cc(err)
res.send('video insert ok')
})
}
})
} else if (imgType.indexOf(type) !== -1){
const imgSql = `select picture from patient where id = ?`
db.query(imgSql, info.id, (err, result) => {
if (err) return res.cc(err)
if (result.length !== 1) return res.cc('查询失败')
const pInsertSql = `update patient set picture = ?`
if (!result[0].picture) {
db.query(pInsertSql, JSON.stringify(fileList), (err, result) => {
if (err) return res.cc(err)
return res.cc('图片上传成功', 200)
})
} else if (JSON.parse(result[0].picture.length)) {
const sqlData = JSON.parse(result[0].picture)
for (let i = 0; i < fileList.length; i++) {
const item = fileList[i]
sqlData.unshift(item)
}
db.query(pInsertSql, JSON.stringify(sqlData), (err, result) => {
if (err) return res.cc(err)
res.cc('image update ok', 200)
})
}
})
} else {
res.cc('上传文件格式不正确,请重新上传!!!')
}
}
结果如下:
- 数据库
- 移动后的结果
|