如何創建一個函式,從服務器獲取準確的影像檔案,然后將其轉換為 base 64,最后將其存盤到變數中。
注意:確切的影像檔案(無需遍歷目錄來列出所有檔案)。
uj5u.com熱心網友回復:
您可以使用image-to-base64庫來做到這一點。
安裝
npm i image-to-base64
用法
const imageToBase64 = require('image-to-base64');
//or
//import imageToBase64 from 'image-to-base64/browser';
imageToBase64("path/to/file.jpg") // Path to the image
.then(
(response) => {
console.log(response); // "cGF0aC90by9maWxlLmpwZw=="
}
)
.catch(
(error) => {
console.log(error); // Logs an error if there was one
}
)
使用 base64 顯示影像
<img src="data:image/jpg;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/447604.html
標籤:javascript 节点.js 表示
