欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

從零開始學習Node.js系列教程三:圖片上傳和顯示方法示例

 更新時間:2017年04月13日 10:50:45   作者:MIN飛翔  
這篇文章主要介紹了Node.js圖片上傳和顯示方法,結合實例形式分析了nodejs基于http傳輸圖片文件及顯示圖片的相關實現(xiàn)步驟與操作技巧,需要的朋友可以參考下

本文實例講述了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:

知識點:

其中用到了fs模塊的readFile讀取文件,它有同步和異步兩個版本。node.js中,并不是所有的API都提供了異步和同步版本,node.js不鼓勵使用同步I/O。

//this is async 異步
/*
 fs.readFile調用時所做的工作只是將異步式I/O請求發(fā)送給了操作系統(tǒng),然后立即返回并執(zhí)行后面的語句,執(zhí)行完以后進入事件循環(huán)監(jiān)聽事件。
 當fs接收到I/O請求完成的事件時,事件循環(huán)會主動調用回調函數(shù)以完成后續(xù)工作。
 */
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程序設計有所幫助。

相關文章

  • Nodejs下DNS緩存問題淺析

    Nodejs下DNS緩存問題淺析

    本文給大家一起探討nodejs下dns的緩存問題,本文給大家介紹的非常詳細,感興趣的朋友一起看看吧
    2016-11-11
  • 基于Node.js的http模塊搭建HTTP服務器

    基于Node.js的http模塊搭建HTTP服務器

    這篇文章主要為大家介紹了基于Node.js的http模塊來搭建HTTP服務器的示例過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2022-02-02
  • express中創(chuàng)建 websocket 接口及問題解答

    express中創(chuàng)建 websocket 接口及問題解答

    本文主要介紹了express中創(chuàng)建 websocket 接口及問題解答,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-05-05
  • node.js中 cluster 模塊和 worker_threads 模塊示例詳解

    node.js中 cluster 模塊和 worker_threads 模塊示例

    這篇文章主要介紹了node.js中 cluster 模塊和 worker_threads 模塊,本文通過示例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2024-05-05
  • node.js報錯:Cannot find module ''ejs''的解決辦法

    node.js報錯:Cannot find module ''ejs''的解決辦法

    最近發(fā)現(xiàn)了node.js居然報錯了,錯誤提示為:Cannot find module 'ejs',后來找了找資料發(fā)現(xiàn)解決的方法其實很簡單,下面通過這篇文章來一起看看吧,希望對同樣遇到這個問題的朋友們能有所幫助。
    2016-12-12
  • NodeJs 文件系統(tǒng)操作模塊fs使用方法詳解

    NodeJs 文件系統(tǒng)操作模塊fs使用方法詳解

    這篇文章主要介紹了NodeJs 文件系統(tǒng)操作模塊fs使用方法,需要的朋友可以參考下
    2018-11-11
  • ChatGPT編程秀之最小元素的設計示例詳解

    ChatGPT編程秀之最小元素的設計示例詳解

    這篇文章主要為大家介紹了ChatGPT編程秀之最小元素的設計示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • Node.js和Vue的安裝與配置超詳細步驟(推薦)

    Node.js和Vue的安裝與配置超詳細步驟(推薦)

    使用VUE前端框架開發(fā),需要安裝Node.js和Vue.js,這篇文章主要給大家介紹了關于Node.js和Vue的安裝與配置超詳細步驟的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2024-01-01
  • Mac 安裝 nodejs方法(圖文詳細步驟)

    Mac 安裝 nodejs方法(圖文詳細步驟)

    這篇文章主要介紹了Mac 安裝 nodejs方法(圖文詳細步驟),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • npx的使用及原理分析

    npx的使用及原理分析

    這篇文章主要介紹了npx的使用及原理,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02

最新評論