摘要:给上传文件重命名,获取添加后缀名允许最大层文件上传国际化工具类房源附件文件服务文件上传文件文件名相对路径对应值文件大小后面写到前端的时候再说怎么调用
multer文件上传
https://github.com/expressjs/...
在博客系统中会涉及到文件上传,这时候需要用到 multer文件上传
model层/**
* 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;
config层 系统配置
router层
service层
/**
* 文件服务
* 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;
}
}
controller层文件上传
/**
* 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调用
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/104097.html
摘要:多一个技能多一条出路,祝你在自学道路上越走越好,掌握自己的核心技能,不只是优秀,还要成为不可替代的人 NodeJs+Express+Mysql + Vuejs 项目实战 最近准备写一系列文章,全面讲述如何基于NodeJs + Express + Mysql + Vuejs 从零开发前后端完全分离项目; 文笔及技术可能在某些方面欠佳,请您指正,共同学习进步 前端:Vuejs全家桶 后端:...
摘要:从本章开始,正式学习如何使用搭建一个博客。但通常我们都会有许多环境,如本地开发环境测试环境和线上环境等,不同的环境的配置不同,我们不可能每次部署时都要去修改引用或者。会根据环境变量的不同从当前执行进程目录下的目录加载不同的配置文件。 从本章开始,正式学习如何使用 Nodejs + Express + Mysql 搭建一个博客。 开发环境 首先说下开发环境安装的核心依赖版本: Node....
摘要:跨域跨域请求也可以使用代表允许所有此时咱们启动服务,在接口调用时就会有跨域白名单拦截校验 跨域corshttps://github.com/expressjs/... var config = { cors: { //跨域请求 origin: [http://localhost:5000, http://localhost:5001],//也可以使用*代表允许...
国际化i18nhttps://github.com/mashpie/i1... showImg(https://segmentfault.com/img/bVbr9I9?w=1850&h=948); 业务使用: showImg(https://segmentfault.com/img/bVbr9Jp?w=1330&h=598); en.json { success: success, ...
阅读 2680·2021-11-23 10:04
阅读 1902·2021-09-02 15:21
阅读 1145·2019-08-30 15:44
阅读 1231·2019-08-30 10:48
阅读 890·2019-08-29 17:21
阅读 3741·2019-08-29 13:13
阅读 2180·2019-08-23 17:17
阅读 1938·2019-08-23 17:04