Nodejs 文件上传事例

//server.js
var http = require("http");
var url = require("url");
function start(route, handle) {
var onRequest = function(request, response) {
	var pathname = url.parse(request.url).pathname;
	console.log("Request for " + pathname + " received.");
	var postData = "";
	
	/*
	request.addListener("data", function(postDataChunk){
		postData += postDataChunk;
		console.log("Reserved POST data chunk'" + postDataChunk + "'.");
	});
	request.addListener("end", function() {
		route(handle, pathname, response, postData);
	});
	*/
	
	route(handle, pathname, response, request);
	
	//route(handle, pathname, response);
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}

exports.start = start;

 

//router.js
function route(handle, pathname, response, request)
{
	if(typeof(handle[pathname]) === "function") {
		console.log("About to route a request for " + pathname);
		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;

 

//requestHandlers.js
var exec = require("child_process").exec;
var querystring = require("querystring"), 
	fs = require("fs"),
	formidable = require("formidable");
	
function sleep(milliSeconds) {
	var startTime = new Date();
	while(new Date().getTime() < startTime + milliSeconds);
}
	
function start(response, request) {
	console.log("Request handler 'start' was called. ");
	
	/*
	exec("dir", 
		{timeout: 10000, maxBuffer: 20000*1024},
		function(error, stdout, stderr) {
			response.writeHead(200, {"Content-Type" : "text/plain"});
			response.write(stdout);
			response.end();
		});
		*/
		
	var body = '<html>' +
		'<head>' + 
		'<meta http-equiv="Content-Type" content="text/html; ' +
		'charset=UTF-8" />' + 
		'</head>' + 
		'<body>' + 
		'<form action="/upload" method="post" enctype="multipart/form-data" >' +
		'<textarea name="text" rows="20" cols="60"></textarea> ' + 
		'<input type="file" name="upload" multiple="multiple" />' +
		'<input type="submit" value="Submit Text" /> ' + 
		'</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. ");
	
	var form = formidable.IncomingForm();
	form.uploadDir = "tmp";
	console.log("about to parse");

	form.parse(request, function(error, fields, files) {
		console.log("parsing done");
		//sleep(10000);
		
		setTimeout(function() {
			try {
				fs.renameSync(files.upload.path, "tmp/test.png");
			} catch(e) {
				console.log(e);
			}
			console.log(files.upload.path);
			response.writeHead(200, {"Content-Type" : "text/html"});
			response.write("received image:<br />");
			response.write("<img src='/show' />");
			response.end();}, 1000);
	});

}

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;

 

//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);

 

 

 

 参考:http://www.nodebeginner.org/index-zh-cn.html

 

 

你可能感兴趣的:(nodejs)