日期:2014-05-16  浏览次数:20346 次

node.js的文件上传
首先为大家推荐一些nodejs的有关资源.
node.js中文社区: http://cnodejs.org/
Node.js入门教程(E文): http://nodebeginner.org/
中文用户手册: http://cnodejs.org/cman/
node.js API文档: http://cnodejs.org/api/
npm依赖管理工具(node 中的RubyGems):  http://npmjs.org/
Node Cloud(全面的node.js资料库): http://www.nodecloud.org/


1.什么是node.js
   Node.js是一个服务器端框架,基于Google的V8 JavaScript引擎创建,旨在利用事件触发、非阻塞的I/ O帮助开发人员构建高度可伸缩的网络程序
2.代码
var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");
var handle = {};   //包含处理函数,此处不能声明为 var handle = [];原因:[]的index只接受数字。{}的index才支持任意可hash的类型。
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show;
server.start(router.route,handle);


var http = require("http");
var url = require("url");
var formidable = require("formidable");  //必须要下在formidable模块,可以使用npm下载

function start(route,handle){
	function onRequest(req,res){
		pathname = url.parse(req.url).pathname;   //得到路径
		var postData = "";
		console.log("Pathname--------------:" +  pathname + " is coming");
		route(handle,req,res,pathname);   //调用路由函数
	}
	http.createServer(onRequest).listen(8080);
	console.log("server is starting");

};

exports.start = start;  //暴露start函数
	

function route(handle,req,res,pathname){
	console.log("route a request for: " + pathname)
	if(typeof handle[pathname] === 'function'){   
		handle[pathname](req,res);
		return;
	}else{
		console.log("No Request Path----???????");
		res.writeHead(200,{"Content-Type":"text/html"});
		res.write("404 Not Found");
		res.end();
	}
}
exports.route = route;


var querystring = require("querystring"),
	fs = require("fs"),
	exec = require("child_process").exec,
	formidable = require("formidable");
function start(req,res){
	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="UploadPic" />'+
        '</form>'+
        '</body>'+
        '</html>';
        
    res.writeHead(200, {'Content-Type' : 'text/html'})
    res.write(body);
    res.end();
}
function upload(req,res){
	console.log("Upload:" + req + "   ----  ===");
	var form = new formidable.IncomingForm();
	form.parse(req,function(error,fields,files){
		console.log("parsing is over");
		fs.renameSync(files.upload.path,"/tmp/test.png");
		res.writeHead(200,{"Content-Type":"text/html"});
		res.write("received image:<br/>");
		res.write("<img src='/show'/>");
		res.end();
	});;
}
function show(req,res){
	console.log("show");
	fs.readFile("/tmp/test.png","binary",function(error,file){
		if(error){
			res.writeHead(500,{"Content-Type":"text/plain"});
			res.write(error+"\n");
			res.end();
		}else{
			 res.writeHead(200,{"Content-Type":"image/jpg"});
			 res.write(file,"binary");
			 res.end();
		}
	});
}

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



Administrator@WIN-23C1Q4GKQ4G ~
$ node /example/index.js
server is starting
Pathname--------------:/start is coming
route a request for: /start
Request handler 'start' was called.
Pathname--------------:/upload i