download.js: Client-Side File Download
Download.js is a client-side JavaScript library, currently at version 1.4.7, designed to programmatically trigger file downloads within the user's browser without requiring server interaction. It provides a single `download()` function that can accept various data types, including URLs, plain strings, Blobs, Typed Arrays, and dataURLs, abstracting the complexities of browser-specific download mechanisms. The library aims to set the filename and MIME type via browser capabilities, behaving similarly to a server's `Content-Disposition` header. While the release cadence is infrequent, the library remains active, offering a lightweight solution for in-browser file generation and download. Its key differentiators lie in its broad data input flexibility and wide browser compatibility, albeit with notable limitations on older browsers and devices lacking local file system access or specific web APIs like `Blob` or `window.URL`.
Common errors
-
File not downloaded on mobile (e.g., iOS)
cause The device lacks a local filesystem or sufficient browser support to handle client-side downloads programmatically.fixThis is often a fundamental device limitation. The library cannot circumvent the absence of a file system. Inform users of this limitation. -
Downloaded file has a generic name (e.g., 'download.bin', 'unknown')
cause The user's browser is an older version or a legacy device that does not support the `download` attribute on anchor tags, which is used to suggest a filename.fixUpdate browser if possible, or accept that custom filenames may not be honored on very old systems. The content type should still be correct. -
download() called with a Blob or TypedArray argument fails
cause The browser in use does not have full support for the Blob API.fixCheck the browser's compatibility for the Blob API. For environments lacking Blob support, you might need to convert data to a dataURL string as a fallback, although this has size limitations.
Warnings
- gotcha Devices without a local filesystem (e.g., iPhone, iPad, Wii) cannot save files using this library.
- gotcha Older browsers or legacy devices that do not support the `a[download]` attribute will only be able to download a few hundred kilobytes of data, and may not honor the custom filename provided.
- gotcha Browser support for Blobs and TypedArrays is required to download these data types. Devices without Blob support will fail when attempting to download them.
- gotcha Callback mode for AJAX operations will not work correctly with vanilla XMLHttpRequest or when dealing with binary files, as it expects specific success callback arguments.
- gotcha Android support for the built-in browser begins at version 4.2. Chrome 36+ and Firefox 20+ on Android 2.3+ generally work well. Older Android versions may have limited or no functionality.
Install
-
npm install downloadjs -
yarn add downloadjs -
pnpm add downloadjs
Imports
- download
const { download } = require('downloadjs');const download = require('downloadjs'); - download
download(data, strFileName, strMimeType);
- download
require(['path/to/downloadjs'], function(download) { download(data, strFileName, strMimeType); });
Quickstart
if (typeof download === 'function') {
console.log('download.js is available!');
// Download a plain text file
download('Hello, Checklist Day!', 'greeting.txt', 'text/plain');
// Download an HTML file with bold text from a Blob
const htmlContent = '<h1>Welcome!</h1><p>This is <b>bold</b> text.</p>';
download(new Blob([htmlContent]), 'welcome.html', 'text/html');
// Download a file from a data URL
const dataUrl = 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><circle cx="50" cy="50" r="40" fill="red" /></svg>';
download(dataUrl, 'red-circle.svg', 'image/svg+xml');
console.log('Check your downloads folder for greeting.txt, welcome.html, and red-circle.svg');
} else {
console.error('download.js function not found. Ensure the script is loaded.');
}