我正在使用电子在离线模式下运行 Vue 应用程序。应用程序使用的很多图像文件都来自云服务,所以我注册了一个文件协议,拦截了这些请求,在本地下载并使用回调从本地存储加载。其他图像文件正在工作,例如 pngs 和 jpgs,但 SVG 文件不在电子中呈现。下面是我的代码。
app.on('ready', () => {
// I have changed the url to contain remote for the cloud files with Vue
protocol.registerFileProtocol('remote', async (request, callback) => {
var resolvePath = request.url.replace('remote', 'https') // change back to https
let address = url.parse(resolvePath, true).pathname;
// get file information
const pathArray = address.split('/')
const assetId = pathArray[1]
const scanId = pathArray[pathArray.length - 2]
const file = pathArray.pop()
if (file.includes('.jpg') || file.includes('.png') || file.includes('.svg')) {
// check asset is saved
var asset = path.resolve(__dirname, 'downloadedAssets', assetId, 'scans', scanId, file)
if (fs.existsSync(asset)) { // if it exists use local image
console.log('image file exists')
resolvePath = asset
} else {
// download method to save file which returns file path
var download = await singleDownload(resolvePath, 'downloadedAssets/' + assetId + '/scans/' + scanId)
// resolve file path
resolvePath = download
}
}
// resolve paths
callback({ path: resolvePath })
})
createWindow()
})
还有什么我需要为 SVG 做的吗我已经检查了调试网络工具中的预览窗口我可以看到 svg 图像并且在响应中我看到了文件数据,内容类型也是 image/svg+xml .我不知道为什么这不是在电子中呈现,而是在 vue 应用程序中呈现。