node.js 通過post上傳圖片並顯示

來源:互聯網
上載者:User

標籤:style   http   tar   ext   color   com   

首先編寫最底層的請求處理常式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://網址:連接埠號碼訪問。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.