服务端代码:

 var express = require('express');
var swig = require('swig');
//1、引入multer模块
var multer = require('multer');
var fs = require('fs');
var path = require('path');
var redis = require('./modules/redis'); var app = express();
app.use(express.bodyParser());
//设置swig模板方法;
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views'); // Using Multer for file uploads.
//2、配置multer中间件参数;
app.use(multer({ dest: './uploads/'}));
//3、设置路由参数,原理就是通过第2步中的中间件后,req.files会带上传过来的图片信息;
app.post('/form',function(req,res){ //console.log(req.files.image); // 上传的文件信息
//console.log(req.body);
//console.log(req.files); var des_file = __dirname + "/uploads/" + req.files.image.originalFilename;
console.log(__dirname);
console.log(req.files.image.originalFilename);
console.log(des_file); //4、图片上传进来后,被保存在内存路径中(个人理解,很重要,姑且这样理解);
fs.readFile( req.files.image.path, function (err, data) {
console.log(data) //5、通过fs模块读取图片保存的内存路径,并将图片读出的信息,保存到设置好的目录里;
fs.writeFile(des_file, data, function (err) {
if( err ){
console.log( err );
}else{
response = {
message:'File uploaded successfully',
filename:req.files.image.originalFilename
};
}
//console.log( response );
res.end( JSON.stringify( response ) );
});
});
}); app.post('/',function(req,res){
if(!(req.body.owner&&req.body.type&&req.body.content)){
if(req.body.type&&(["male","female"].indexOf(req.body.type) === -1)){
return res.json({code:0,msg:"类型错误"})
}
return res.json({code:0,msg:"信息不完整"})
}
redis.throw(req.body,function(result){
res.json(result);
})
}); app.get('/',function(req,res){
console.log(req.query);
if(!req.query.user){
return res.json({code:0,msg:"信息不完整"});
}
if(req.query.type && (["male","female"].indexOf(req.query.type) === -1)){
return res.json({code:0,msg:"类型错误"});
}
redis.pick(req.query,function(result){
res.json(result);
})
});
app.get('/form',function(req,res){
res.render('form');
}); //require("./test/test.js").b();
//
//
//require("./test/test.js").a();
//var cc = require("./test/test");
//cc.b();
//cc.a(); app.listen(3000);

前端form表单代码:

 <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<!--form若是上传图片必须带上这个属性 enctype="multipart/form-data"-->
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="owner" value="123131414"/>
<input type="file" name="image"/>
<input type="submit" value="上传文件" />
</form>
</body>
</html>

最重要的一点是:

<input type="file" name="image"/>

必须加上 name="image"
必须加上 name="image"
必须加上 name="image"

重要的事情说三遍

nodejs上传图片模块做法;的更多相关文章

  1. nodejs事件模块

    nodejs 事件模块 events 只有一个对象 EventEmitter . var EventEmitter = require('events').EventEmitter;var life ...

  2. 配置 Windows 下的 nodejs C++ 模块编译环境

    根据 node-gyp 指示的 Windows 编译环境说明, 简单一句话就是 "Python + VC++ 编译环境". 所有需要的安装文件, 我都下载好放到百度云盘了: nod ...

  3. NodeJS http 模块

    #4 NodeJS http 模块 工作目录 server.js var http = require('http'); var fs = require('fs'); var path = requ ...

  4. nodejs的模块系统(实例分析exprots和module.exprots)

    前言:工欲善其事,必先利其器.模块系统是nodejs组织管理代码的利器也是调用第三方代码的途径,本文将详细讲解nodejs的模块系统.在文章最后实例分析一下exprots和module.exprots ...

  5. nodejs cluster模块初探

    大家都知道nodejs是一个单进程单线程的服务器引擎,不管有多么的强大硬件,只能利用到单个CPU进行计算.所以,为了使用多核cpu来提高性能 就有了cluster,让node可以利用多核CPU实现并行 ...

  6. NodeJS Web模块

    NodeJS Web模块 本文介绍nodeJS的http模块的基本用法,实现简单服务器和客户端 经典Web架构 Client:客户端一般指浏览器,通过HTTP协议向服务器发送请求(request) S ...

  7. 配置 Windows 下的 nodejs C++ 模块编译环境 安装 node-gyp

    配置 Windows 下的 nodejs C++ 模块编译环境 根据 node-gyp 指示的 Windows 编译环境说明, 简单一句话就是 "Python + VC++ 编译环境&quo ...

  8. nodejs cheerio模块提取html页面内容

    nodejs cheerio模块提取html页面内容 1. nodejs cheerio模块提取html页面内容 1.1. 找到目标元素 1.2. 美化文本输出 1.3. 提取答案文本 1.4. 最终 ...

  9. es6 中的模块导入与nodejs 中模块的导入的异同!

    我们知道es6 的模块导入导出是通过import 和 export 来实现,而nodejs的模块导入导出是通过require 和module.exports 来实现,那么它们有什么异同吗? 请看如下: ...

随机推荐

  1. php pdo mysql数据库操作类

    <?php namespace iphp\core; use iphp\App; /** * 数据库操作基类 基于pdo * @author xuen * 支持链式操作,支持参数绑定 * 说明1 ...

  2. JQuery DataTables Editor---只修改页面内容

    近来在工作中需要对JQuery DataTables进行增,删,改的操作,在网上找了一些资料,感觉比较的好的就是(http://editor.datatables.net/)文章中所展示的操作方法(如 ...

  3. linux arp攻击解决方法 测试很有效

    公司有台centos服务器中了arp攻击,严重影响业务,测试了很多方法都没解决,机房技术也没法处理. 通过下面方法,可以有效抵挡arp攻击.   1.环境 centos6.4   2.执行 arpin ...

  4. tempo 2.0 学习记录

    最近在做项目时使用了tempo,感觉还不错,但是发现网上对于tempo 2.0 的介绍比较少,我也是在GitHub才找到了比较完整的使用说明,我也简单记录一下自己的使用过程,重新学习一下tempo 2 ...

  5. SSM整合中遇到的不能扫描注解的bug

    我的开发环境为: ubuntu14.04LTS 64bit; Spring Tool Suite  3.5.0.RELEASE Maven 3.2.3 SSM整合中遇到的不能扫描注解的bug 最终解决 ...

  6. docker 基于现有镜像修改后保存,上传私有仓库

    docker:/root# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f3cb864be528 192.168.3 ...

  7. libcurl提交表单上传文件

    不多说了,curl的http上传文件代码示例,有需要的可以参考. int http_post_file(const char *url, const char *user, const char *p ...

  8. aix puppet agent

    demo控制脚本tel,150 5519 8367 Running Puppet on AIX Puppet on AIX is… not officially supported, yet stil ...

  9. div简单布局理解

    以下是div的理解

  10. iOS 唯一设备号

    https://github.com/fabiocaccamo/FCUUID 目前比较好的解决方案.