-
Notifications
You must be signed in to change notification settings - Fork 5
Description
Feature Request
Plugin
Capacitor Filesystem
Description
The downloadFile method in the Capacitor Filesystem plugin on the web platform currently writes the file to the IndexedDB instead of directly downloading it to the Downloads folder. This behavior was introduced as a fix to a previous issue (#1716) and was implemented in pull request #1724. However, there are use cases where the old behavior (downloading the file directly to the Downloads folder) is preferred. I propose that an option be added to the downloadFile method to allow developers to choose whether the file should be written to the IndexedDB or downloaded directly to the Downloads folder.
Platform(s)
Web
Preferred Solution
Add an optional parameter to the downloadFile method that determines where the file should be saved. If the parameter is set to true, the file should be written to the IndexedDB. If it's set to false, the file should be downloaded directly to the Downloads folder.
Alternatives
An alternative solution could be to create a separate method for downloading the file directly to the Downloads folder. This would keep the downloadFile method's behavior consistent across all platforms, while still providing developers with the flexibility to choose where the file should be saved on the web platform.
Additional Context
Currently, to achieve the old behavior, developers need to manually read the file from the IndexedDB, create a Blob from the file data, create an object URL from the Blob, and then create a hidden anchor element to trigger the download. This workaround is not ideal as it adds unnecessary complexity to the code.
Here's an example of the current workaround:
const result: DownloadFileResult = await Filesystem.downloadFile({
url,
path: `${fileName}`,
directory: Directory.Documents,
})
if (!result.blob) {
throw new Error('No blob found');
}
const blobUrl = URL.createObjectURL(result.blob);
const link = document.createElement('a');
link.href = blobUrl;
link.download = fileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(blobUrl);
This feature request aims to eliminate the need for this workaround by providing built-in support for downloading files directly to the Downloads folder on the web platform.