Client-side file downloading using JS and HTML5
download(data, strFileName, strMimeType);
- The Blob, File, String, or dataURL containing the soon-to-ba File's contents.
- 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.
- 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.
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 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.
The 4kb script is available to download or view online or Fork on GitHub
Change Log (v4)
- 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
- 2014 :: v4 adds AMD/UMD, commonJS, and plain browser support
- 20XX :: ???? Considering Zip, Tar, and other multi-file outputs, Blob.prototype.download option, and more, stay tuned folks.
This work is licensed under a Creative Commons Attribution 4.0 International License, attribute to "dandavis".