nodejs上传图片模块做法;
服务端代码:
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上传图片模块做法;的更多相关文章
- nodejs事件模块
nodejs 事件模块 events 只有一个对象 EventEmitter . var EventEmitter = require('events').EventEmitter;var life ...
- 配置 Windows 下的 nodejs C++ 模块编译环境
根据 node-gyp 指示的 Windows 编译环境说明, 简单一句话就是 "Python + VC++ 编译环境". 所有需要的安装文件, 我都下载好放到百度云盘了: nod ...
- NodeJS http 模块
#4 NodeJS http 模块 工作目录 server.js var http = require('http'); var fs = require('fs'); var path = requ ...
- nodejs的模块系统(实例分析exprots和module.exprots)
前言:工欲善其事,必先利其器.模块系统是nodejs组织管理代码的利器也是调用第三方代码的途径,本文将详细讲解nodejs的模块系统.在文章最后实例分析一下exprots和module.exprots ...
- nodejs cluster模块初探
大家都知道nodejs是一个单进程单线程的服务器引擎,不管有多么的强大硬件,只能利用到单个CPU进行计算.所以,为了使用多核cpu来提高性能 就有了cluster,让node可以利用多核CPU实现并行 ...
- NodeJS Web模块
NodeJS Web模块 本文介绍nodeJS的http模块的基本用法,实现简单服务器和客户端 经典Web架构 Client:客户端一般指浏览器,通过HTTP协议向服务器发送请求(request) S ...
- 配置 Windows 下的 nodejs C++ 模块编译环境 安装 node-gyp
配置 Windows 下的 nodejs C++ 模块编译环境 根据 node-gyp 指示的 Windows 编译环境说明, 简单一句话就是 "Python + VC++ 编译环境&quo ...
- nodejs cheerio模块提取html页面内容
nodejs cheerio模块提取html页面内容 1. nodejs cheerio模块提取html页面内容 1.1. 找到目标元素 1.2. 美化文本输出 1.3. 提取答案文本 1.4. 最终 ...
- es6 中的模块导入与nodejs 中模块的导入的异同!
我们知道es6 的模块导入导出是通过import 和 export 来实现,而nodejs的模块导入导出是通过require 和module.exports 来实现,那么它们有什么异同吗? 请看如下: ...
随机推荐
- 用gson 解 json
1.json 简单介绍 轻量级数据存储传输文件,比xml精简. 2.Gson简单介绍 android中解json方式很多,比较常用json-lib goole-Gson. json-lib要包含的包 ...
- 【自学php】第四天 - 使用数组
php支持两种数组,数字索引数组和关联数组.关联数组有点类似Map,可以用字符串或其他数据类型做键对应相应的值保存在数组中. 1.初始化数组 数字索引数组的初始化可以使用如下代码: $products ...
- Spring 3.x企业实用开发实战(1)
有关Spring的介绍这里就不赘述了,主要是学习了陈雄华版的<Spring 3.x企业应用开发实战>并做了一点笔记,以助于后期的回顾和复习. 废话不多说,直接进入主题,以下所有代码基于&l ...
- InputStream和OutputStream
1.在java中stream代表一种数据流(源),javaio的底层数据元,---(想像成水龙头) 2.任何有能力产生数据流(源)的javaio对象就可以看作是一个InputStream对象既然它能产 ...
- aix knowlgdgecenter
http://www-01.ibm.com/support/knowledgecenter/ssw_aix_53/com.ibm.aix.install/doc/insgdrf/HT_insgdrf_ ...
- hdu 3934 Summer holiday(凸包最大内接三角形)
求n个点能组成的最大三角形,一发旋转卡壳模板题... #include<algorithm> #include<iostream> #include<cstring> ...
- .NET(C#):使用反射来获取枚举的名称、值和特性【转】
首先需要从内部了解一下枚举(Enumeration),相信许多人已经知道了,当我们声明一个这样的枚举类型: enumMyEnum { AAA, BBB, CCC } 背后的IL是这样的: .class ...
- jquery动态连接节点
<1> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- UART串口协议基础1
Louis kaly.liu@163.com 串口协议基础 1 串口概述 串口由收发器组成.发送器是通过TxD引脚发送串行数据,接收器是通过RxD引脚接收串行数据. 发送器和接收器都利用了一个移位寄存 ...
- DevExpress中ChartControl柱状图(Bar)用法
我的数据 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 chartControl1.Series.Clear(); ...