Node.js formidable 上传文件的实现

本教程介绍如何使用Node.js实现文件上传与处理,包括安装Node.js、配置防火墙、使用第三方库formidable简化文件处理过程,并通过示例展示如何在服务器端接收、保存并展示上传的文件。

本案例来自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.

转载于:https://www.cnblogs.com/lianche/p/4048553.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值