multer文件上傳
https://github.com/expressjs/...
在博客系統中會涉及到文件上傳,這時候須要用到 multer文件上傳前端
/** * Attachment附件表 * @type {[type]} */ var Sequelize = require('sequelize'); var Mysql = require('./mysql'); var Attachment = Mysql.define('attachment', { uuid: { type: Sequelize.UUID, allowNull: false, primaryKey: true, defaultValue: Sequelize.UUIDV1,//Sequelize.UUIDV4 }, //uuid name: Sequelize.STRING, //附件名字 relativeUrl: Sequelize.STRING, //附件相對地址 absoluteUrl: Sequelize.STRING, //附件絕對地址 type: Sequelize.STRING(2), //附件類型(1圖片、2視頻、3音頻、4其餘文件) }, { freezeTableName: true, // 自定義表名 tableName: 'Attachment', timestamps: true, // 添加時間戳屬性 (updatedAt, createdAt) createdAt: 'createDate',// 將createdAt字段改個名 updatedAt: 'updateDate',// 將updatedAt字段改個名 indexes: [{ // 索引 type: 'UNIQUE', method: 'BTREE', unique: true, //惟一 fields: ['uuid'], }], }); module.exports = Attachment;
/** * 文件服務 * add by wwj * 2019-05-04 12:03:39 */ var fs = require('fs'); var path = require('path'); //路徑 var uuid = require('node-uuid'); //uuid var Promise = require("bluebird"); var multer = require('multer'); //文件上傳 var config = require('config-lite'); //配置 module.exports = { /** * 獲取年月 */ getYearMonth: function() { var fdate = new Date(); return fdate.getFullYear() + '' + (fdate.getMonth() + 1) + '' + fdate.getDate(); }, /** * 鏈接文件存放路徑 * type文件對應類型 好比文章對應article * filename 文件名含後綴名 */ createFilePath: function(pathType, filename) { var that = this; var fpath = path.join(__dirname, '../public/attchments', (pathType || 'default'), that.getYearMonth()); if (!fs.existsSync(fpath)) { fs.mkdirSync(fpath); } if (filename) { return fpath + '/' + filename; } else { return fpath; } }, /** * 處理文件上傳 */ setFileUpload: function(opts) { var that = this; var storage = multer.diskStorage({ //設置上傳後文件路徑,uploads文件夾會自動建立。 destination: function(req, file, cb) { cb(null, that.createFilePath(opts.pathType)) }, //給上傳文件重命名,獲取添加後綴名 filename: function(req, file, cb) { var fileFormat = file.originalname.split("."); // cb(null, file.originalname + '_' + Math.ceil(Math.random()*9) + Date.now() + "." + fileFormat[fileFormat.length - 1]); cb(null, uuid() + "." + fileFormat[fileFormat.length - 1]); }, }); var upload = multer({ limits: { fileSize: config.file.limit.fileSize[opts.pathType] || config.file.limit.fileSize.default, //容許最大 }, storage: storage, }); return upload; } }
/** * common controllers * add by wwj * 2016-12-22 17:45:53 */ var co = require('co'); var Promise = require("bluebird"); var i18n = require('i18n'); //國際化 var utils = require('../libs/utils'); //工具類 var Attachment = require('../models/index').Attachment; //房源附件 var fileService = require('../services/file'); //文件服務 module.exports = { /** * 文件上傳 */ uploadEnclosure: function(req, res, next) { //文件s var files = req.files; if (!files || !files.length) { //err utils.handleError({ response: res, error: i18n.__('uploadFileFail'), }); return; } co(function*() { //all var fileResult = yield Promise.all(files.map(function(file) { return Attachment.create({ name: file.originalname, //文件名 relativeUrl: file.filename, //相對路徑 absoluteUrl: fileService.getFilePath("default", file.filename), type: fileService.handlerFileType(file.mimetype), //對應int值 size: file.size, //文件大小 }); })); //success utils.handleJson({ response: res, msg: i18n.__('uploadFileSuccess'), result: { fileList: fileResult, }, }); }).catch(function(error) { //err utils.handleError({ response: res, error: error, }); }); }, };
後面寫到前端的時候再說 怎麼ajax fileupload調用node