AngualrJs 最新 文件图片上传教程7:Server-controllers
来源:互联网 发布:caffe windows教程 编辑:程序博客网 时间:2024/05/16 17:47
womeninformationmanagement.server.controller.js
'use strict';/** * Module dependencies */var _ = require('lodash'), fs = require('fs'), path = require('path'), config = require(path.resolve('./config/config')), multer = require(path.resolve('./config/private/multer')), errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller')), sequelize = require(path.resolve('./config/lib/sequelize')), logger = require(path.resolve('./config/lib/logger')) .getLogger_FileNameBase(__filename);//创建接收头像对象var uploadImage = new multer('womenfile', 4 * 1024 * 1024, /image/, '.doc');//创建目录uploadImage.mkPaths();/** * Create an womenInformationManagement */exports.create = function (req, res) { var WomenInformationManagement = sequelize.model('WomenInformationManagement'); var womenInformationManagement = WomenInformationManagement.build(req.body); var newingImageUrl; var newingPhoto1ImageUrl; if (womenInformationManagement) { uploadImage.recv(req, res, [{name: 'file_path'}, {name: 'photo'}]) .then(updateUserInfo) .then(function () { res.json(womenInformationManagement); }) .catch(function (err) { logger.error('上传文件失败:', err); res.status(422).send(err); }); } else { res.status(401).send({ message: 'womenInformationManagement is not exist' }); } function updateUserInfo(files) { return new Promise(function (resolve, reject) { if (womenInformationManagement) { if (files && files.file_path && files.file_path.length === 1) { womenInformationManagement.file_path = path.join(uploadImage.mountDir, files.file_path[0].filename).replace(/\\/g, '/'); newingImageUrl = womenInformationManagement.file_path; } if (files && files.photo && files.photo.length === 1) { womenInformationManagement.photo = path.join(uploadImage.mountDir, files.photo[0].filename).replace(/\\/g, '/'); newingPhoto1ImageUrl = womenInformationManagement.photo; } womenInformationManagement.title = req.body.title; womenInformationManagement.type = req.body.type; // womenInformationManagement.file_content = req.body.file_content; womenInformationManagement.time_update = req.body.time_update; // womenInformationManagement.photo = req.body.photo; womenInformationManagement.remark = req.body.remark; //图片 womenInformationManagement.save().then(function () { resolve(); }).catch(function (err) { reject(err); }); } else { reject(new Error('no upload')); } }); }};/** * Show the current womenInformationManagement */exports.read = function (req, res) { var womenInformationManagement = req.model ? req.model.toJSON() : {}; womenInformationManagement.isCurrentUserOwner = !!(req.user && womenInformationManagement.user && womenInformationManagement.user.id.toString() === req.user.id.toString()); res.json(womenInformationManagement);};/** * Update an womenInformationManagement */exports.update = function (req, res) { var womenInformationManagement = req.model; var newingFileUrl; var existingFileUrl; var newingPhotoImageUrl; var existingPhotoImageUrl; if (womenInformationManagement) { existingFileUrl = womenInformationManagement.file_path; existingPhotoImageUrl = womenInformationManagement.photo; uploadImage.recv(req, res, [{name: 'file_path'}, {name: 'photo'}]) .then(updateUserInfo) .then(deleteOldImage) .then(function () { res.json(womenInformationManagement); }) .catch(function (err) { logger.error('recv upload womenInformationManagement file err:', err); res.status(422).send(err); }); } else { res.status(401).send({ message: 'womenInformationManagement is not exist' }); } function updateUserInfo(files) { return new Promise(function (resolve, reject) { if (womenInformationManagement) { if (files && files.file_path && files.file_path.length === 1) { womenInformationManagement.file_path = path.join(uploadImage.mountDir, files.file_path[0].filename).replace(/\\/g, '/'); newingFileUrl = womenInformationManagement.file_path; } if (files && files.photo && files.photo.length === 1) { womenInformationManagement.photo = path.join(uploadImage.mountDir, files.photo[0].filename).replace(/\\/g, '/'); newingPhotoImageUrl = womenInformationManagement.photo; } womenInformationManagement.title = req.body.title; womenInformationManagement.type = req.body.type; // womenInformationManagement.file_content = req.body.file_content; womenInformationManagement.time_update = req.body.time_update; // womenInformationManagement.photo = req.body.photo; womenInformationManagement.remark = req.body.remark; //图片 womenInformationManagement.save().then(function () { resolve(); }).catch(function (err) { reject(err); }); } else { reject(new Error('no upload')); } }); } //删除旧照片 function deleteOldImage() { return new Promise(function (resolve, reject) { if (existingPhotoImageUrl && newingPhotoImageUrl) { var oldimgname = existingPhotoImageUrl.replace(uploadImage.mountDir, uploadImage.diskDir); fs.unlink(oldimgname, function (unlinkError) { if (unlinkError) { resolve(); } else { resolve(); } }); } else { resolve(); } if (existingFileUrl && newingFileUrl) { var oldfile = existingFileUrl.replace(uploadImage.mountDir, uploadImage.diskDir); fs.unlink(oldfile, function (unlinkError) { if (unlinkError) { resolve(); } else { resolve(); } }); } else { resolve(); } }); }};/** * Delete an womenInformationManagement */exports.delete = function (req, res) { var womenInformationManagement = req.model; womenInformationManagement.destroy().then(function () { res.json(womenInformationManagement); }).catch(function (err) { return res.status(422).send({ message: errorHandler.getErrorMessage(err) }); });};/** * List of WomenInformationManagement */exports.list = function (req, res) { var WomenInformationManagement = sequelize.model('WomenInformationManagement'); WomenInformationManagement.findAll({ limit: [0, 20], order: 'id ASC' }).then(function (womenInformationManagement) { return res.jsonp(womenInformationManagement); }).catch(function (err) { logger.error('womenInformationManagement list error:', err); return res.status(422).send(err); });};//----分页function listByPage(req, res, limit, offset) { var WomenInformationManagement = sequelize.model('WomenInformationManagement'); WomenInformationManagement.findAll({ limit: [limit, offset], order: 'id ASC' }).then(function (commMemberTable) { return res.jsonp(commMemberTable); }).catch(function (err) { logger.error('WomenInformationManagement list error:', err); return res.status(422).send(err); });}//---------总数function listCount(req, res) { var sql = 'select count(*) sum from WomenInformationManagement'; sequelize.query(sql, {type: sequelize.QueryTypes.SELECT}).then(function (infos) { res.jsonp(infos); }).catch(function (err) { logger.error('listCount error:', err); return res.status(422).send(err); });}/** * WomenInformationManagement middleware */exports.womenInformationManagementByID = function (req, res, next, id) { var WomenInformationManagement = sequelize.model('WomenInformationManagement'); var limit = parseInt(req.query.limit, 0);//(pageNum-1)*20 var offset = parseInt(req.query.offset, 0);//20 每页总数 if (offset !== 0 && id === '0') { listByPage(req, res, limit, offset); } else if (limit === 0 && offset === 0 && id === '0') { listCount(req, res); } else if (id !== '0') { WomenInformationManagement.findOne({ where: {id: id} }).then(function (womenInformationManagement) { if (!womenInformationManagement) { logger.error('No womenInformationManagement with that identifier has been found'); return res.status(404).send({ message: 'No womenInformationManagement with that identifier has been found' }); } req.model = womenInformationManagement; next(); }).catch(function (err) { //return next(err); logger.error('womenInformationManagement ByID error:', err); res.status(422).send({ message: errorHandler.getErrorMessage(err) }); }); }};
阅读全文
0 0
- AngualrJs 最新 文件图片上传教程7:Server-controllers
- AngualrJs 最新 文件图片上传教程6:Server-config
- AngualrJs 最新 文件图片上传教程8:Server-models
- AngualrJs 最新 文件图片上传教程9:Server-policies
- AngualrJs 最新 文件图片上传教程10:Server-routes
- AngualrJs 最新 文件图片上传教程1:Client-View
- AngualrJs 最新 文件图片上传教程2:Client-Controller-modal
- AngualrJs 最新 文件图片上传教程3:Client-Controller-curdtable
- AngualrJs 最新 文件图片上传教程4:Client-Services
- AngualrJs 最新 文件图片上传教程0:Client-module
- AngualrJs 最新 文件图片上传教程5:Client-css Client-config
- angualrjs上传文件 后端springmvc用file接收
- yii图片上传教程
- SpringMVC教程--图片上传
- 最新上传程序(傻瓜式教程)
- ckeditor 图片上传最新完美处理
- android最新的选择图片及上传
- 文件上传,图片上传案例
- Ubuntu终端常见几种字体颜色表示的意义
- jhipster与cas整合简介
- Linux 下搭建iscsi共享存储
- 聊一聊用户体验
- SQL SERVER中强制类型转换cast和convert的区别
- AngualrJs 最新 文件图片上传教程7:Server-controllers
- 10大基础实用算法
- CSS——关于继承性和层叠性的练习
- Swift 下标脚本
- SpringMVC拦截器配置及使用方法
- Apache 创建虚拟主机目录和设置默认访问页面
- 安卓属性动画与自定义控件实现圆球移动并实现颜色渐变
- 【转】素数判断四方法
- 金蝶kis版本差异