簡體   English   中英

如何使用Express從mysql顯示圖像

[英]How to display an image from mysql using Express

我有一個SQL表“動物”,上面有斑點圖像。 我發現了如何上傳圖片,但沒有找到如何展示它們。

我想在表中顯示稱為“狗”的圖像。 這是我的代碼,在其中打印blob img的結果。

let sql = 'SELECT * FROM animals WHERE file_name=\'Dog\''
    connection.query(sql, (err,result) => {
        console.log(result[0].img)

    })

這是我的代碼的結果:

<Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff db 00 84 00 09 06 07 13 12 12 15 13 13 13 16 16 15 15 18 18 17 16 18 15 17 15 17 17 16 ... >

有什么辦法可以顯示那張照片? 謝謝。

您可以使用Fetch API在網頁上獲取資源。

您可以這樣顯示圖像:

      fetch('http://localhost:1234/your_api')
      .then(function(response) {
           return response.blob();
      })
      .then(function(myBlob) {
           var objectURL = URL.createObjectURL(myBlob); 
           document.querySelector("#databaseimage").src = objectURL;
      });

在HTML中:

   <img id="databaseimage"/>

您可以在這里閱讀有關Fetch API更多信息: https : //developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

暫無
暫無

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

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