node.js学习-在页面上传图片并显示例子

1、服务器启动js, 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);

2、服务器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(8888);
   console.log("Server has started.");

}

exports.start = start;

3、路由选择route.js

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

exports.route = route;

 

4、请求处理程序requestHandler.js(使用node-formidable外部模块来处理文件上传,注意:要在index.js文件所在位置增加tmp文件夹,用于存放上传的图片)

var exec = require("child_process").exec;
var fs = require("fs");
var formidable = require("formidable");
function start(response, postData){
 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" method="post" enctype="multipart/form-data" >' +
        '<textarea name="text" rows="20" cols="60"></textarea>' +
        '<textarea name="text2" rows="20" cols="60"></textarea>' +
        '<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");
 //return "Hello Upload";
 var form = new formidable.IncomingForm();
 form.uploadDir = "./tmp";
 console.log("about to parse");
 form.parse(request, function(error, fields, files){
  console.log("parsing done" + files.upload.path); 
  fs.renameSync(files.upload.path, "/tmp/test.png");
   response.writeHead(200, {"Content-Type":"text/html"});
   response.write('<head><meta charset="utf-8"/></head>');
   response.write("<img src='/show'/>");
   response.end();
 });
  
}

function show(response, postData){
 console.log("Request handler 'show' was called.");
 fs.readFile('/tmp/test.png', "binary", function(error, file){
      if(error){
        console.log("Request handler 'show' error:" + 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;

注:

 

posted @ 2013-05-01 19:47  wangle100  阅读(499)  评论(0编辑  收藏  举报