簡體   English   中英

圖像無法在Node.js中使用GridFS顯示服務圖像

[英]Image not display serving image using GridFS in Node.js

我正在從AFNetworking接收一個png文件,並將其保存到GridFS,然后我希望能夠在某個時候對其進行檢索。 出於好奇,我在進入GridFS之前記錄了圖像,看起來像..

<89504e47 0d0a1a0a 0000000d 49484452 00000074 0000008c 08020000 0022391a   ...>

我將其保存在緩沖區中,然后將其存儲到GridFS中。

當我通過GET請求檢索它時,我再次記錄它,然后將其發送出去,並且它似乎是相同的格式。 然后我嘗試這樣做

res.writeHead(200, {'Content-Type': 'image/png' });
gs.createReadStream(image).pipe(res); //using GridJS this it the syntax to read

在瀏覽器中查看時,它看起來像是空白或損壞的圖像鏈接。 如果我檢查頁面源,它​​似乎只是

如果我從不設置標題,它將顯示為數百行

<89504e47 0d0a1a0a 0000000d 49484452 00000074 0000008c 08020000 0022391a   ...>

我覺得我沒有在轉換緩沖權之類的東西。

var http = require('http'),
   MongoDB = require("mongodb"),
   MongoClient = require("mongodb").MongoClient,
   GridStore = require("mongodb").GridStore;

http.createServer(function (req, res) {
    console.log("Serving request for file: " + req.url);
    MongoClient.connect("mongodb://localhost:27017/test", {}, function(err, db) {
        gridStore = new GridStore(db, req.url, "r");
        gridStore.open(function(err, gs) {
            if (err) {
                console.log("error in open: " + err);
                return;
            }

            res.writeHead(200, {'Content-Type': 'image/png'});
            var s = gs.stream(true);
            s.pipe(res);
        });
    });

}).listen(8124, "127.0.0.1");

console.log('Server running at http://127.0.0.1:8124/');

我可以使用上面粘貼的代碼成功運行服務器並將png文件成功提供給瀏覽器。 但是,既然如此,您說的是當您執行“查看源代碼”時,源的原始內容確實出現在客戶端上。 我建議嘗試我編寫的代碼,看看是否有相同的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM