├── LICENSE.md ├── README.md ├── config.js └── fileupload.js /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 amor 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nodejs-file-upload-multer 2 | 3 | multer文件上传实例 4 | 5 | package version: 6 | 7 | "multer": "^1.1.0", 8 | "md5": "^2.1.0", 9 | 10 | 11 | 12 | 使用方法: 13 | 14 | ``` 15 | var express = require('express'); 16 | var router = express.Router(); 17 | var upload = require('./fileupload'); 18 | //文件上传服务 19 | router.post('/upload', upload.single('avatar'), function (req, res, next) { 20 | if (req.file) { 21 | res.send('文件上传成功') 22 | console.log(req.file); 23 | console.log(req.body); 24 | } 25 | }); 26 | ``` 27 | 28 | form: 29 | 30 | ``` 31 |
32 | 33 | 选择图片: 34 | 35 |
36 | ``` 37 | -------------------------------------------------------------------------------- /config.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * @Description 系统配置 4 | * @Author Amor 5 | * @Created 2016/04/26 11:48 6 | * 技术只是解决问题的选择,而不是解决问题的根本... 7 | * 我是Amor,为发骚而生! 8 | * 9 | */ 10 | 11 | module.exports = { 12 | upload: { 13 | path: process.cwd() + '/uploads' 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /fileupload.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * @Description 文件上传配置 4 | * @Author Amor 5 | * @Created 2016/04/27 17:50 6 | * 技术只是解决问题的选择,而不是解决问题的根本... 7 | * 我是Amor,为发骚而生! 8 | * 9 | */ 10 | var multer = require('multer'); 11 | var md5 = require('md5'); 12 | var config = require('./config') 13 | 14 | var storage = multer.diskStorage({ 15 | //设置上传文件路径,以后可以扩展成上传至七牛,文件服务器等等 16 | //Note:如果你传递的是一个函数,你负责创建文件夹,如果你传递的是一个字符串,multer会自动创建 17 | destination: config.upload.path, 18 | //TODO:文件区分目录存放 19 | //给上传文件重命名 20 | filename: function (req, file, cb) { 21 | var fileFormat = (file.originalname).split("."); 22 | cb(null, file.fieldname + "." + fileFormat[fileFormat.length - 1]); 23 | } 24 | }); 25 | 26 | //添加配置文件到muler对象。 27 | var upload = multer({ 28 | storage: storage, 29 | //其他设置请参考multer的limits 30 | //limits:{} 31 | }); 32 | //导出对象 33 | module.exports = upload; 34 | --------------------------------------------------------------------------------