码迷,mamicode.com
首页 > Web开发 > 详细

Node.js formidable 上传文件的实现

时间:2014-10-24 18:28:41      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   使用   

本案例来自Node.js入门书籍:

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

 

示例中只能上传并展示png图片,当然其他文件都是可行的,自己微调一下即可。

 

安装node.js:

# curl -sL https://rpm.nodesource.com/setup | bash -

# yum install -y nodejs 

 

防火墙打开8888端口;

 

Node.js自身处理上传文件会非常繁琐,可使用第三方的node-formidable来轻松处理:

npm install formidable

 

以下是代码:

 

Server端:

#server.js
var http = require("http"); var url = require("url"); var order = 1; //请求次数 function start(route, handle) { function onRequest(request, response) { var postData = ""; var pathname = url.parse(request.url).pathname; //请求位置 console.log("Request for " + pathname + " received."); route(handle, pathname, response, request); //路由此次请求 console.log("Reauest received ~ +" + order); //请求次数 order++; } http.createServer(onRequest).listen(8888); //监听8888端口 console.log("Server has started"); } exports.start = start;

 

路由:

#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/plain" }); response.write("404 Not found"); response.end(); } } exports.route = route;

 

Handler:

#requestHandlers.js
var querystring = require("querystring"); var formidable = require("formidable"); var fs = require("fs"); 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."); var form = new formidable.IncomingForm(); console.log("about to parse"); form.parse(request, function(error, fields, files) { console.log("parsing done"); fs.renameSync(files.upload.path, "/tmp/test.png"); //更名 response.writeHead(200, { "Content-Type": "text/html" }); response.write("received image:<br/>"); response.write("<img src=‘/show‘ />"); //调用show方法展示图片 response.end(); }); } function show(response) { var imagePath = "/tmp/test.png"; console.log("Request handler ‘show‘ was called."); fs.readFile(imagePath, "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["/"] = handle["/start"] = requestHandlers.start; handle["/upload"] = requestHandlers.upload; handle["/show"] = requestHandlers.show; server.start(router.route, handle); //初始函数 - 参数为路由方法及相应的处理方法

 

将这些扔到一个文件夹中,启动:

# node index.js

 

浏览器访问:http://yourhost:8888 或在其后加上/start 即可进入上传页面。

 

 

End.

Node.js formidable 上传文件的实现

标签:des   style   blog   http   color   io   os   ar   使用   

原文地址:http://www.cnblogs.com/lianche/p/4048553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!