欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

从零开始学习Node.js系列教程三:图片上传和显示方法示例

程序员文章站 2022-04-29 16:37:31
本文实例讲述了node.js图片上传和显示方法。分享给大家供大家参考,具体如下: index.js var server = require("./server...

本文实例讲述了node.js图片上传和显示方法。分享给大家供大家参考,具体如下:

index.js

var server = require("./server");
var router = require("./router");
var requesthandlers = require("./requesthandlers");
var handle = {}
handle["/"] = requesthandlers.start;
handle["/start"] = requesthandlers.start;
handle["/upload"] = requesthandlers.upload;
handle["/show"] = requesthandlers.show;
server.start(router.route, handle);

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(3000);
 console.log("server has started.");
}
exports.start = start;

requesthandlers.js

var querystring = require("querystring"),
  fs = require("fs"),
  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" 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();
  form.uploaddir = "d:\\min\\nodejsexample2\\tmp";
  console.log("about to parse1");
  form.parse(request, function(error, fields, files) {
    console.log("parsing done");
    console.log(files.upload.path);
    fs.renamesync(files.upload.path, "d:\\min\\nodejsexample2\\tmp\\test.png");
    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("d:\\min\\nodejsexample2\\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.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;

result:

从零开始学习Node.js系列教程三:图片上传和显示方法示例

从零开始学习Node.js系列教程三:图片上传和显示方法示例

从零开始学习Node.js系列教程三:图片上传和显示方法示例

知识点:

其中用到了fs模块的readfile读取文件,它有同步和异步两个版本。node.js中,并不是所有的api都提供了异步和同步版本,node.js不鼓励使用同步i/o。

//this is async 异步
/*
 fs.readfile调用时所做的工作只是将异步式i/o请求发送给了操作系统,然后立即返回并执行后面的语句,执行完以后进入事件循环监听事件。
 当fs接收到i/o请求完成的事件时,事件循环会主动调用回调函数以完成后续工作。
 */
var fs = require('fs');
fs.readfile('file.txt', 'utf-8', function(err, data){
  if (err){
    console.error(err);
  } else {
    console.log(data);
  }
});

//this is sync 同步
var fs = require('fs');
var data = fs.readfilesync('file.txt', 'utf-8');
console.log(data);
console.log('end.');

希望本文所述对大家nodejs程序设计有所帮助。