@tater-archives/file-dl
v1.0.2
Published
Simple download file utility for browser
Downloads
1
Readme
@tater-archives/file-dl
A simple utility to create a file from a string and download it in the browser.
Example Usage
With an <a>
element:
// Get the anchor
const downloadAnchor = document.getElementById('download');
// Generate URL
const uri = getDownloadURI('Lorem ipsum dolor sit amet');
// Set the URL
downloadAnchor.href = uri;
// Set the filename
downloadAnchor.download = 'lorem.txt';
As a triggered event:
// Get the button
const downloadButton = document.getElementById('download');
// Add an event to download the file on click
downloadButton.addEventListner('click', () => {
download('Lorem ipsum dolor sit amet', 'lorem.txt');
});
You can use it in the browser via esm.sh or unpkg.
import { download, getDownloadURI } from 'https://esm.sh/@tater-archives/file-dl';
// or
import { download, getDownloadURI } from 'https://unpkg.com/@tater-archives/file-dl';
WARNING: When using the download()
function, Chrome will prevent
downloading of files in quick succession if not triggered by a user event, or
downloading simultaneously. It is recommended to use a button or anchor if you
want to download multiple files.