download.js: Client-Side File Download

1.4.7 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of the global `download` function to save various types of client-side generated files like text, HTML, and SVG.

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.');
}

view raw JSON →