码迷,mamicode.com
首页 > Web开发 > 详细

nodejs上传图片模块做法;

时间:2016-02-19 01:37:56      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

服务端代码:

 1 var express = require(‘express‘);
 2 var swig = require(‘swig‘);
 3 //1、引入multer模块
 4 var multer = require(‘multer‘);
 5 var fs = require(‘fs‘);
 6 var path = require(‘path‘);
 7 var redis = require(‘./modules/redis‘);
 8 
 9 var app = express();
10 app.use(express.bodyParser());
11 //设置swig模板方法;
12 app.engine(‘html‘, swig.renderFile);
13 app.set(‘view engine‘, ‘html‘);
14 app.set(‘views‘, __dirname + ‘/views‘);
15 
16 // Using Multer for file uploads.
17 //2、配置multer中间件参数;
18 app.use(multer({ dest: ‘./uploads/‘}));
19 //3、设置路由参数,原理就是通过第2步中的中间件后,req.files会带上传过来的图片信息;
20 app.post(‘/form‘,function(req,res){
21 
22     //console.log(req.files.image);  // 上传的文件信息
23     //console.log(req.body);
24     //console.log(req.files);
25 
26     var des_file = __dirname + "/uploads/" + req.files.image.originalFilename;
27     console.log(__dirname);
28     console.log(req.files.image.originalFilename);
29     console.log(des_file);
30 
31 //4、图片上传进来后,被保存在内存路径中(个人理解,很重要,姑且这样理解);
32     fs.readFile( req.files.image.path, function (err, data) {
33         console.log(data)
34 
35 //5、通过fs模块读取图片保存的内存路径,并将图片读出的信息,保存到设置好的目录里;
36         fs.writeFile(des_file, data, function (err) {
37             if( err ){
38                 console.log( err );
39             }else{
40                 response = {
41                     message:‘File uploaded successfully‘,
42                     filename:req.files.image.originalFilename
43                 };
44             }
45             //console.log( response );
46             res.end( JSON.stringify( response ) );
47         });
48     });
49 });
50 
51 
52 app.post(‘/‘,function(req,res){
53     if(!(req.body.owner&&req.body.type&&req.body.content)){
54         if(req.body.type&&(["male","female"].indexOf(req.body.type) === -1)){
55             return res.json({code:0,msg:"类型错误"})
56         }
57         return res.json({code:0,msg:"信息不完整"})
58     }
59     redis.throw(req.body,function(result){
60         res.json(result);
61     })
62 });
63 
64 app.get(‘/‘,function(req,res){
65     console.log(req.query);
66     if(!req.query.user){
67         return res.json({code:0,msg:"信息不完整"});
68     }
69     if(req.query.type && (["male","female"].indexOf(req.query.type) === -1)){
70         return res.json({code:0,msg:"类型错误"});
71     }
72     redis.pick(req.query,function(result){
73         res.json(result);
74     })
75 });
76 app.get(‘/form‘,function(req,res){
77     res.render(‘form‘);
78 });
79 
80 //require("./test/test.js").b();
81 //
82 //
83 //require("./test/test.js").a();
84 //var cc = require("./test/test");
85 //cc.b();
86 //cc.a();
87 
88 app.listen(3000);

前端form表单代码:

 1 <!doctype html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8 <!--form若是上传图片必须带上这个属性  enctype="multipart/form-data"-->
 9 <form action="" method="post" enctype="multipart/form-data">
10     <input type="text" name="owner" value="123131414"/>
11     <input type="file" name="image"/>
12     <input type="submit" value="上传文件" />
13 </form>
14 </body>
15 </html>

 

nodejs上传图片模块做法;

标签:

原文地址:http://www.cnblogs.com/suoking/p/5199672.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!