nodejs----上传显示图片

Node.js实现图片的上传和显示,需要用到外部模块,formidable,这个模块是对解释上传的文件数据很好的抽象。文件上传,就是”处理“POST数据。

1,安装模块,打开nodejs安装目录,在命令行输入指令:

npm install formidable

2.引用formidable的模块,使用require指令。这个模块就是将通过http post请求提交的表单,在Node.js中可以被解释。

var formidable = require("formidable");

3.需要建立一个新的IncomingForm,对提交的表单的抽象表示,就可以用它解析request对象表单中需要的字。

var form = new formidable.IncomingForm();

4.实现图片的上传和显示这个引用因为要引用到formidable,必须把项目放在nodejs的安装目录下。

5.程序:

-----requestHandlers.js

-----server.js

-----router.js

-----index.js

requestHandlers.js

/*requestHandlers.js*/
var querystring = require("querystring"), fs = require("fs"), formidable = require ("formidable");

function start(response){
	console.log("Request handler ‘start‘ was called");

	var body = ‘<html>‘ +
	‘<head>‘ +
	‘<meta http-equiv="Content-Type" content="text/html charset=UTF-8"/>‘ +
	‘</head>‘ +
	‘<body>‘ +
	‘<form action="/upload" enctype="multipart/form-data" method="post">‘ +
	‘<input type="file" name="upload" multiple="multiple"/>‘+
	‘<input type="submit" value="Upload file"/>‘+
	‘</form>‘ +
	‘</body>‘ +
	‘</html>‘;
	response.writeHead(200,{"Content-Type":"text/html"});
	response.write(body);
	response.end();
	}

	function upload(response, request){
		console.log("Request handler ‘upload‘ was called");

		/*formidable外部模块,解析上传的文件数据做了很好的抽象*/
		var form = new formidable.IncomingForm();
		//在应用目录下创建文件夹tmp,设置为上传路径
		form.uploadDir = "tmp"
		console.log("About to parse");
		form.parse(request,function(error,fields, files){
			console.log("parsing done");
			console.log(files.upload.path);
			//renameSync(oldpath,newpath)
			//同步操作,需要try catch
			try{
			fs.renameSync(files.upload.path,"./tmp/test.png");
			}catch(e){
				console.log(e);
			}
			response.writeHead(200,{"Content-Type":"text/html"});
			response.write("received image:<br/>");
			//返回/show处理程序显示图片
			response.write("<img src=‘/show‘/>");
			response.end();
		});
	}

	//显示图片
	function show(response){
		console.log("Request handler ‘show‘ was called.");
		fs.readFile("./tmp/test.png","binary",function(error, file){
			if(error){
				response.writeHead(500,{"Content-Type":"text/plain"});
				response.write(error + "/n");
				response.end();
			}else{
				response.writeHead(200,{"Content-Type":"image/png"});
				response.write(file,"binary");
				response.end();
			}
		});
	}

exports.start = start;
exports.upload = upload;
exports.show = show;

  

router.js

/*router.js*/
/*通过检查给定的路径对应的请求处理程序是否存在,如果存在的话直接调用相应的函数*/

function route(handle,pathname,response,request){
	console.log("About to route a request for " + pathname);
	if(typeof handle[pathname] === ‘function‘){
		handle[pathname](response,request);
	}else{
		console.log("No request handler found for " + pathname);
		response.writeHead(404,{"Content-Type":"text/html"});
		response.write("404 Not found");
		response.end();
	}
}
exports.route = route;

server.js

/*server.js  处理请求模块*/
var http = require("http");
var url = require("url");

function start(route,handle){
	function onRequest(request,response){
		var pathname = url.parse(request.url).pathname;
		console.log("Request for" +  pathname + "Received.");
		route(handle,pathname,response,request);
	}

	http.createServer(onRequest).listen(3000);
	console.log("Server has started");
}

exports.start = start;

  

index.js

/*index.js 主模块*/

var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");

var handle = {};

handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show;

server.start(router.route,handle);

6.启动应用:

node index.js

7.打开URL

http://127.0.0.1:3000

8.上传图片后:

9.图片被保存在路径:

  

 

时间: 2024-07-31 08:05:18

nodejs----上传显示图片的相关文章

Django1.11搭建一个简易上传显示图片的后台

项目展示需要,之前没研究过Django,网上查资料快速做了一个后台,写下了防止自己忘了. p { margin-bottom: 0.1in; line-height: 120% } a:link { } 1. 安装Django: pip install Django 2. 测试Django: python >> import django >>django.VERSION 显示版本为(1, 11, 5, 'final', 0),刚接触,github也没找到能运行起来的. 3. 运行

MVC应用程序显示上传的图片(续)

上一篇<MVC应用程序显示上传的图片>http://www.cnblogs.com/insus/p/3597543.html 最后有提及没有实现用户点击图片,显示原图的功能.此篇Insus.NET来完成它. 想使用一个dialog box来呈现原图.这个jQuery的UI已经有了,引用相关的js类库即可. 代到HomeController.cs控制器,添加一个ContentResult方法: 即是传入图片名称,找到上传目录的相对应的图片.打开ViewUploadPhoto.cshtml 视图,

js 显示刚刚上传的图片 (onchange事件)

<table> <tr width="100"> <td>上传商场图片:</td> <td> <input type="file" name="img" onchange="previewImage(this)"> </td> </tr> <tr height=124px;> <td ></td>

MVC应用程序显示上传的图片

前两篇<MVC应用程序实现上传文件>http://www.cnblogs.com/insus/p/3590907.html和<MVC应用程序实现上传文件(续)>http://www.cnblogs.com/insus/p/3594834.html,我们练习了上传文件,当然上传图片也是一样. 此篇我们练习,怎样在MVC应用程序中显示用户上传的图片.为了接近更真实的练习,Insus.NET决定对以前的程序修改一下,就是上传的目录把原来的Temp目录改为UploadFiles目录.也就是

ssm框架实现图片上传显示并保存地址到数据库

本案例是通过springmvc+spring+mybatis框架以商品上传为例,实现的图片上传功能,并把图片的地址保存到数据库并在前台显示上传的图片. 本项目是使用maven搭建的项目,首先看下项目结构 相关配置自行搜索,下边直接实现上传功能 1.创建数据库 DROP TABLE IF EXISTS `product`; CREATE TABLE `product` ( `pid` int(11) NOT NULL AUTO_INCREMENT, `pimage` varchar(255) DE

magento 1.9 上传后图片前后台无法正常显示

1.上传后图片不显示,设置 允许 flash 2.保证php 执行是内存大小至少为为128M,多种方式设置,这里以init_set为例子,在index.php 加入下面一行代码,根据情况而定 ini_set('memory_limit','128M'); 原文地址:https://www.cnblogs.com/pa200318/p/10241850.html

调起摄像头、上传下载图片、本地展示图片

之前那偏微信JS-SDK授权的文章实现了分享接口,那么这里总结一下如何在微信里面通过js调起原生摄像头,以及上传下载图片. 1.配置 页面引入通过jssdk授权后,传入wx对象,首先配置需要的接口 wx.config({ /* debug: true, */ appId: appid, timestamp: timestamp, nonceStr: nonceStr, signature: signature, jsApiList: [ 'chooseImage',//拍照或从手机相册中选图接口

基于spring-boot的web应用,ckeditor上传文件图片文件

说来惭愧,这个应用调试,折腾了我一整天,google了很多帖子,才算整明白,今天在这里做个记录和分享吧,也作为自己后续的参考! 第一步,ckeditor(本博文论及的ckeditor版本4.5.6)的配置图片文件上传功能,默认这个是没有开启的,就不用多说,ckeditor官网上也说的很清楚!http://docs.ckeditor.com 下面简单的说下配置(配置文件algoConfig.js): 1 CKEDITOR.editorConfig = function( config ) { 2

微信开发之调起摄像头、本地展示图片、上传下载图片

之前那篇微信JS-SDK授权的文章实现了分享接口,那么这里总结一下如何在微信里面通过js调起原生摄像头,以及上传下载图片. 1.配置 页面引入通过jssdk授权后,传入wx对象,首先配置需要的接口 wx.config({ /* debug: true, */ appId: appid, timestamp: timestamp, nonceStr: nonceStr, signature: signature, jsApiList: [ 'chooseImage',//拍照或从手机相册中选图接口

图片上传,图片旋转,拖拽

能够支持IE,谷歌,火狐浏览器(兼容多浏览器不容易啊) 下面仅提供核心思想和部分代码:  拖拽:是使用网上现成的JS代码,在此基础上进行适当的修改即可满足自己的需求,最主要的就是判定拖拽的范围,上传的图片不能给拖没了,所以加上个范围限定,判断超出了这个范围便拖拽无效果. 旋转与缩放要区分浏览器.. 旋转:IE浏览器下想要实现图片的旋转很简单只要调用IE提供的滤镜filter参数为一个旋转矩阵即可.谷歌和火狐浏览器图片显示用的是canvas标签而不是img标签所以图片的旋转需要用canvas标签相