Tauri convertFileSrc
convertFileSrc(: , : ):filePathstringprotocol?stringstring
将设备文件路径转换为可由 Web 视图加载的 URL。 请注意,必须将 和添加到 中的 tauri.security.csp 中。 示例 CSP 值:在图像源上使用资产协议。asset:https://asset.localhosttauri.conf.json"csp": "default-src 'self'; img-src 'self' asset: https://asset.localhost"
此外,必须添加到 tauri.allowlist.protocol 中,并且必须在同一对象上的数组上定义其访问范围。assettauri.conf.jsonassetScopeprotocol
例
import { appDataDir, join } from '@tauri-apps/api/path';
import { convertFileSrc } from '@tauri-apps/api/tauri';
const appDataDirPath = await appDataDir();
const filePath = await join(appDataDirPath, 'assets/video.mp4');
const assetUrl = convertFileSrc(filePath);
const video = document.getElementById('my-video');
const source = document.createElement('source');
source.type = 'video/mp4';
source.src = assetUrl;
video.appendChild(source);
video.load();Since: 1.0.0
参数
| 名字 | 类型 | 默认值 | 描述 |
|---|---|---|---|
filePath | string | undefined | 文件路径。 |
protocol | string | 'asset' | 要使用的协议。缺省值为 .只有在使用自定义协议时才需要设置此项。asset |
Returns: string
可用作 Web 视图上的源的 URL。