Skip to main content

Opening and downloading files

The widget provides special actions for opening and downloading files:

You need to listen to these actions using the api.on() method and call the required functions for these actions.

Example:

<script>
import { Filemanager } from "@wx/svelte-filemanager";

const server = "https://some-backend-url";

function getLink(id, download) {
return server + "/direct?id=" + encodeURIComponent(id) + (download ? "&download=true" : "");
}

let fmApi;
function loadData({ id }) {
fetch(server + "/files/" + encodeURIComponent(id))
.then((data) => data.json())
.then((data) => {
fmApi.exec("provide-data", {
id,
data,
});
});
}

let rawData = [];
$: {
fetch(server + "/files")
.then((data) => data.json())
.then((data) => (rawData = data));
}

function init(api) {
fmApi = api;

api.on("download-file", ({ id }) => {
window.open(getLink(id, true), "_self");
});

api.on("open-file", ({ id }) => {
window.open(getLink(id), "_blank");
});

api.on("request-data", loadData);
}

</script>

<Filemanager {init} data={rawData} />

About loading data see here: Loading data


Related samples: Backend data