node.js 通过post上传图片并显示

首先编写最底层的请求处理程序requestHandler.js,首先要通过npm在项目所在的目录安装formidable,通过语句:npm install formidable安装,然后代码如下:

var querystring = require("querystring");
var fs = require("fs");
var 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">‘+
‘<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");
// response.writeHead(200,{"Content-Type":"text/html"});
// response.write("You have sent the text :"+querystring.parse(postData).text);
// response.end();
var form = new formidable.IncomingForm();
form.uploadDir = "e:/node_test/images/tmp";
console.log("about to parse");
form.parse(request,function(error,fields,files){
console.log("parsing done.");
try{
fs.renameSync(files.upload.path, "e:/node_test/images/test.png");
}catch(e){
console.log(e);
}
response.writeHead(200,{"Content-Type":"text/html"});
response.write("received image:<br>");
response.write("<img src=‘/show‘ />");
response.end();
});
}
function show(response){
console.log("Request handler ‘show‘ was called.");
fs.readFile("e:/node_test/images/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,代码如下:

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:

var http = require("http");
var url = require("url");
function start(route,handle){
function onRequest(request,response){
// var postData = "";
// var pathname = url.parse(request.url).pathname;
// console.log("Request for "+pathname+" received.");

// request.setEncoding("utf8");

// request.addListener("data",function(postDataChunk){
// postData += postDataChunk;
// console.log("Received POST data chunk ‘"+postDataChunk+"‘.");
// });
// request.addListener("end",function(){
// route(handle,pathname,response,postData);
// });
var pathname = url.parse(request.url).pathname;
console.log("Request for "+pathname+" received.");
route(handle,pathname,response,request);
}

http.createServer(onRequest).listen(8899);
console.log("Server has started.");
}
exports.start = start;

然后,编写入口文件index.js,并引入路由、服务器、请求处理程序,代码如下:

var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandler");
var handle = {};
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show;
server.start(router.route, handle);

最后在控制台重启服务器,然后在浏览器刷入http://网址:端口号访问。

node.js 通过post上传图片并显示,布布扣,bubuko.com

node.js 通过post上传图片并显示

上一篇:关于使用jquery时,ie8下提示对象不支持的属性或方法的解决办法


下一篇:HDU 1452 Happy 2004(因子和的积性函数)