Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork4.4k
An HTML5 saveAs() FileSaver implementation
License
eligrey/FileSaver.js
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
If you need to save really large files bigger than the blob's size limitation or don't haveenough RAM, then have a look at the more advancedStreamSaver.jsthat can save data directly to the hard drive asynchronously with the power of the new streams API. That will havesupport for progress, cancelation and knowing when it's done writing
FileSaver.js is the solution to saving files on the client-side, and is perfect forweb apps that generates files on the client, However if the file is coming from theserver we recommend you to first try to useContent-Disposition attachment response header as it has more cross-browser compatiblity.
Looking forcanvas.toBlob()
for saving canvases? Check outcanvas-toBlob.js for a cross-browser implementation.
Browser | Constructs as | Filenames | Max Blob Size | Dependencies |
---|---|---|---|---|
Firefox 20+ | Blob | Yes | 800 MiB | None |
Firefox < 20 | data: URI | No | n/a | Blob.js |
Chrome | Blob | Yes | 2GB | None |
Chrome for Android | Blob | Yes | RAM/5 | None |
Edge | Blob | Yes | ? | None |
IE 10+ | Blob | Yes | 600 MiB | None |
Opera 15+ | Blob | Yes | 500 MiB | None |
Opera < 15 | data: URI | No | n/a | Blob.js |
Safari 6.1+* | Blob | No | ? | None |
Safari < 6 | data: URI | No | n/a | Blob.js |
Safari 10.1+ | Blob | Yes | n/a | None |
Feature detection is possible:
try{varisFileSaverSupported=!!newBlob;}catch(e){}
It is possible to save text files in IE < 10 without Flash-based polyfills.SeeChenWenBrian and koffsyrup'ssaveTextAs()
for more details.
Blobs may be opened instead of saved sometimes—you may have to direct your Safari users to manuallypress⌘+S to save the file after it is opened. Using theapplication/octet-stream
MIME type to force downloadscan cause issues in Safari.
saveAs must be run within a user interaction event such as onTouchDown or onClick; setTimeout will prevent saveAs from triggering. Due to restrictions in iOS saveAs opens in a new window instead of downloading, if you want this fixed pleasetell Apple how this WebKit bug is affecting you.
import{saveAs}from'file-saver';
FileSaversaveAs(Blob/File/Url,optionalDOMStringfilename,optionalObject{autoBom})
Pass{ autoBom: true }
if you want FileSaver.js to automatically provide Unicode text encoding hints (see:byte order mark). Note that this is only done if your blob type hascharset=utf-8
set.
varFileSaver=require('file-saver');varblob=newBlob(["Hello, world!"],{type:"text/plain;charset=utf-8"});FileSaver.saveAs(blob,"hello world.txt");
varblob=newBlob(["Hello, world!"],{type:"text/plain;charset=utf-8"});FileSaver.saveAs(blob,"hello world.txt");
FileSaver.saveAs("https://httpbin.org/image","image.jpg");
Using URLs within the same origin will just usea[download]
.Otherwise, it will first check if it supports cors header with a synchronous head request.If it does, it will download the data and save using blob URLs.If not, it will try to download it usinga[download]
.
The standard W3C File APIBlob
interface is not available in all browsers.Blob.js is a cross-browserBlob
implementation that solves this.
varcanvas=document.getElementById("my-canvas");canvas.toBlob(function(blob){saveAs(blob,"pretty image.png");});
Note: The standard HTML5canvas.toBlob()
method is not available in all browsers.canvas-toBlob.js is a cross-browsercanvas.toBlob()
that polyfills this.
You can save a File constructor without specifying a filename. If thefile itself already contains a name, there is a hand full of ways to get a fileinstance (from storage, file input, new constructor, clipboard event).If you still want to change the name, then you can change it in the 2nd argument.
// Note: Ie and Edge don't support the new File constructor,// so it's better to construct blobs and use saveAs(blob, filename)varfile=newFile(["Hello, world!"],"hello world.txt",{type:"text/plain;charset=utf-8"});FileSaver.saveAs(file);
# Basic Node.JS installationnpm install file-saver --savebower install file-saver
Additionally, TypeScript definitions can be installed via:
# Additional typescript definitionsnpm install @types/file-saver --save-dev
About
An HTML5 saveAs() FileSaver implementation
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.