nodeJS基礎08:讀取圖片

來源:互聯網
上載者:User

標籤:end   bin   nbsp   圖片   注意   return   err   run   方法   

1.讀取圖片
//server.jsvar http = require("http");var readImage = require("./readImage");http.createServer(function(res, res){    // res.writeHead(200, {"Content-Type":"text/html; charset=uf-8"});    res.writeHead(200, {"Content-Type":"image/jpeg"});    if (res.url!=="/favicon.ico") {        //res.write(‘hello,world‘);//不能向用戶端輸出任何位元組,否則會影響圖片的輸出        readImage.readImage(‘./test.png‘, res); //如果檔案路徑存在則添加資料,如果不存在則建立檔案並且添加資料        console.log("繼續執行");        //res.end(‘end‘); 在 readImage.readImage方法中已經寫過了    }}).listen(8000);console.log(‘Server running at http://127.0.0.1:8000/‘);
// readImage.jsvar  fs=  require(‘fs‘);module.exports={    readImage:function(path,res){        fs.readFile(path,‘binary‘,function(err,  file)  {            if  (err)  {                console.log(err);                return;            }else{                console.log("輸出檔案");                res.writeHead(200,  {‘Content-Type‘:‘image/jpeg‘});                res.write(file,‘binary‘);                res.end();            }        });    }}; 

注意:在讀取圖片的過程不能向頁面寫入任何東西。

現在我們能夠向瀏覽器輸出文字,或者圖片。下一節我們將學習如何向瀏覽器同時輸出文字和圖片。

 

nodeJS基礎08:讀取圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.