Function tauri_sys::tauri::convert_file_src
source · pub async fn convert_file_src(
file_path: &str,
protocol: Option<&str>,
) -> Result<Url, Error>
Expand description
Convert a device file path to an URL that can be loaded by the webview.
Note that asset:
and https://asset.localhost
must be added to tauri.security.csp
in tauri.conf.json
.
Example CSP value: "csp": "default-src 'self'; img-src 'self' asset: https://asset.localhost"
to use the asset protocol on image sources.
Additionally, asset
must be added to tauri.allowlist.protocol
in tauri.conf.json
and its access scope must be defined on the assetScope
array on the same protocol
object.
@param filePath The file path.
@param protocol The protocol to use. Defaults to asset
. You only need to set this when using a custom protocol.
§Example
use tauri_api::path::{app_data_dir, join};
use tauri_api::tauri::convert_file_src;
const app_data_dir_path = app_data_dir().await;
const file_path = join(app_data_dir_path, "assets/video.mp4").await;
const asset_url = convert_file_src(file_path);
let window = web_sys::window().expect("no global `window` exists");
let document = window.document().expect("should have a document on window");
// Manufacture the element we're gonna append
let video = document.get_element_by_id("my-video")?;
let source = document.create_element("source")?;
source.set_attribute("type", "video/mp4")?;
source.set_attribute("src", asset_url.as_str())?;
video.append_child(&val)?;
@return the URL that can be used as source on the webview.