NODEJS(3)File Upload Sample

NODEJS(3)File Upload Sample

Serving something useful
Handling POST Requests
Add a textarea in start request handler in requestHandlers.js:
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" content="text/html; '+
'charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" method="post">'+
'<textarea name="text" rows="20" cols="60"></textarea>'+
'<input type="submit" value="Submit text" />'+
'</form>'+
'</body>'+
'</html>';

response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();


We can add listeners in request as follow:
request.addListener("data", function(chunk){
//called when a new chunk of data was received
});

request.addListener("end", function() {
//called when all chunks of data have been received
});

Put the data and end event callbacks in the server, collecting all POST data chunks in the data callback, and calling the router upon receiving the end event, while passing the collected data chunks on to the router, which in turn passes it on to the request handlers.

Changes in server.js
function onRequest(request, response) {
var postData = "";
…snip...
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);
});
…snip...

We did three things in sever.js, we defined that we expect the encoding of the received data to be UTF-8.
We add an event listener for the "data" event which step by step fills our new postData variable whenever a new chunk of POST data arrives.
When all POST data is gathered, we will call the end event to pass the data to router.

We will change the router.js and display the content.
function route(handle, pathname, response, postData) {
…snip…
handle[pathname](response, postData);
…snip…

And in requestHandlers.js, we include the data in our response of the upload request handler:
function upload(response, postData) {
…snip…
response.write("You've sent: " + postData);
…snip…

We will use querystring module to parse the postData.
var querystring = require("query string");
…snip…
querystring.parse(postData).text
…snip…

Handling file uploads
how to use an external module. The name of the external module is node-formidable. We need to install other modules in NPM.
>npm install formidable

Then we can try to use this in requestHandlers.js
var fs = require("fs");
var formidable = require("formidable");
…snip…
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" method="post" enctype="multipart/form-data">'+
'<input type="text" name="title" value="hello" /> <br/>'+
'<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.");

var form = new formidable.IncomingForm();
console.log("about to parse");
form.parse(request, function(error, fields, files){
console.log("parsing done");

/* Possible error on windows system:
tried to rename to an already existing file */
fs.rename(files.upload.path, "/tmp/test.png", function(err) {
if(err){
fs.unlink("/tmp/test.png");
fs.rename(files.upload.path, "/tmp/test.png");
}
});
response.writeHead(200, {"Content-Type": "text/html"});
response.write("received image:<br />");
response.write(fields.title + "<br />");
response.write("<img src='/show' />");
response.end();

});
}


function show(response){
console.log("Request handler 'show' was called.");
fs.readFile("/tmp/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.show = show;

In other file, router.js, server.js, index.js, we will pass the parameter request like response. And we will add show handler to our requestHandlers
route(handle, pathname, response, request);

handle[pathname](response, request);

handle["/show"] = requestHandlers.show;

references:
http://www.nodebeginner.org/#serving-something-useful
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值