我必須將靜態影像發送到另一個 Node 應用程式
為此,我需要從檔案中獲取 Base64
這是我在另一個專案(VueJS web app)中使用的功能:
export async function getBase64FromURL (path, filename) {
const fr = new FileReader()
fr.readAdDataURL(new File(path, filename))
return await new Promise((resolve, reject) => {
fr.onloadend = () => {
resolve(fr.result)
}
})
}
NodeJS 缺少一些功能,例如FileReader()我發現了這個npm 包
但是我還沒有找到 new File() 的解決方案,我該怎么辦?
uj5u.com熱心網友回復:
// Import "fs" lib.
const fs = require('fs');
// Read file as buffer and convert to BASE64.
const fileBase64 = fs.readFileSync('./file.txt').toString('base64');
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/511785.html
