What is downloadjs?
The downloadjs npm package is a simple utility for triggering file downloads in the browser. It allows you to programmatically download files of various types, such as text, images, and binary data, directly from JavaScript.
What are downloadjs's main functionalities?
Download Text Files
This feature allows you to download text content as a file. The code sample demonstrates how to create a text file named 'hello.txt' with the content 'Hello, world!' and trigger its download.
const download = require('downloadjs');
const text = 'Hello, world!';
download(text, 'hello.txt', 'text/plain');
Download Images
This feature allows you to download images. The code sample fetches an image from a URL, converts it to a Blob, and triggers its download as 'image.png'.
const download = require('downloadjs');
const imageUrl = 'https://example.com/image.png';
fetch(imageUrl)
.then(response => response.blob())
.then(blob => download(blob, 'image.png', 'image/png'));
Download Binary Data
This feature allows you to download binary data. The code sample demonstrates how to create a binary file named 'binary.bin' with some sample binary data and trigger its download.
const download = require('downloadjs');
const binaryData = new Uint8Array([0x00, 0x01, 0x02, 0x03]);
download(binaryData, 'binary.bin', 'application/octet-stream');
Other packages similar to downloadjs
file-saver
The file-saver package is a well-known library for saving files on the client-side. It provides similar functionality to downloadjs, allowing you to save text, images, and binary data. However, file-saver is more widely used and has better support for various file types and browsers.
browser-filesaver
The browser-filesaver package is another alternative for saving files in the browser. It offers similar capabilities to downloadjs, such as saving text and binary data. It is a lightweight library and can be a good choice for projects that need a simple solution for file downloads.
download
========
Summary
The download() function is used to trigger a file download from JavaScript.
It specifies the contents and name of a new file placed in the browser's download directory. The input can be a URL, String, Blob, or Typed Array of data, or via a dataURL representing the file's data as base64 or url-encoded string. No matter the input format, download() saves a file using the specified file name and mime information in the same manner as a server using a Content-Disposition HTTP header.
Getting and Using
Via NPM/Bower
npm install downloadjs
bower install downloadjs
require("downloadjs")(data, strFileName, strMimeType);
Simple global download
function via <script>
include
download(data, strFileName, strMimeType);
Included via AMD
require(['path/to/file'], function(download) {
download(data, strFileName, strMimeType);
});
Parameters
- data - The Blob, File, String, or dataURL containing the soon-to-be File's contents.
- strFileName - The name of the file to be created. Note that older browsers (like FF3.5, Ch5) don't honor the file name you provide, instead they automatically name the downloaded file.
- strMimeType - The MIME content-type of the file to download. While optional, it helps the browser present friendlier information about the download to the user, encouraging them to accept the download.
Example Usage
Plain Text
download("hello world", "dlText.txt", "text/plain");
download("data:text/plain,hello%20world", "dlDataUrlText.txt", "text/plain");
download(new Blob(["hello world"]), "dlTextBlob.txt", "text/plain");
download("/robots.txt");
text UInt8 Array - live demo
var str= "hello world", arr= new Uint8Array(str.length);
str.split("").forEach(function(a,b){
arr[b]=a.charCodeAt();
});
download( arr, "textUInt8Array.txt", "text/plain" );
HTML
download(document.documentElement.outerHTML, "dlHTML.html", "text/html");
download(new Blob(["hello world".bold()]), "dlHtmlBlob.html", "text/html");
(note that callback mode won't work on vanilla ajax or with binary files)
$.ajax({
url: "/download.html",
success: download.bind(true, "text/html", "dlAjaxCallback.html")
});
Binary Files
download("/diff6.png");
Image via ajax for custom filename - live demo
var x=new XMLHttpRequest();
x.open( "GET", "/diff6.png" , true);
x.responseType="blob";
x.onload= function(e){download(e.target.response, "awesomesauce.png", "image/png");};
x.send();
Compatibility
download.js works with a wide range of devices and browsers.
You can expect it to work for the vast majority of your users, with some common-sense limits:
- Devices without file systems like iPhone, iPad, Wii, et al. have nowhere to save the file to, sorry.
- Android support starts at 4.2 for the built-in browser, though chrome 36+ and firefox 20+ on android 2.3+ work well.
- Devices without Blob support won't be able to download Blobs or TypedArrays
- Legacy devices (no a[download]) support can only download a few hundred kilobytes of data, and can't give the file a custom name.
- Devices without window.URL support can only download a couple megabytes of data
- IE versions of 9 and before are NOT supported because the don't support a[download] or dataURL frame locations.
FAQ
Can I tell when a download is done/canceled?
No.How can I style the temporary download link?
Define CSS class styles for .download-js-link
.What's up with Safari?
I don't know either but pull requests that improve the situation are welcome.Why is my binary file corrupted?
Likely: an incorrect MIME or using jQuery ajax, which has no bin support.How big of files work?
Depends, try yourself: File Echo Demo... I do a 1GB dl routinely on a thinkpad...
Change Log (v4.1)
- 2008 :: landed a FF+Chrome compat way of downloading strings to local un-named files, upgraded to use a hidden frame and optional mime
- 2012 :: added named files via a[download], msSaveBlob() for IE (10+) support, and window.URL support for larger+faster saves than dataURLs
- 2014 :: added dataURL and Blob Input, bind-toggle arity, and legacy dataURL fallback was improved with force-download mime and base64 support
- 2015 :: converted to amd/commonJS module with browser-friendly fallback
- 2015 :: 4.1 added direct URL downloading via a single URL argument.
- 2016 :: 4.2 added large dataURL support, a more semantic codebase, and hidden temp links
- 20XX :: ???? Considering Zip, Tar, and other multi-file outputs, Blob.prototype.download option, and more, stay tuned folks.