Skip to main content

download-file

Description

Fires when downloading a file or folder

The event is triggered after the download operation is initiated but before the operation is executed.

Usage

"download-file": ({
id: string
}) => void | false;

Parameters

The callback of the download-file action takes an object with the following parameters:

  • id - (required) the ID of a file that is downloaded

Returning false from the event handler will block the operation in question, which can be used, for example, to prevent downloading certain files. You can do it via the api.intercept() method.

Example

The next example shows how to configure the downloading of a file. About loading data from the server see here: Loading data

<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" : "");
}

function parseDates(data) {
data.forEach((item) => {
if (item.date) item.date = new Date(item.date * 1000);
});
return data;
}

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

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

function init(api) {
fmApi = api;

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

api.on("request-data", loadData);
}
</script>

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

Related articles: