• Stars
    star
    20,738
  • Rank 1,128 (Top 0.03 %)
  • Language
    JavaScript
  • License
    Other
  • Created almost 13 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An HTML5 saveAs() FileSaver implementation

If you need to save really large files bigger than the blob's size limitation or don't have enough RAM, then have a look at the more advanced StreamSaver.js that can save data directly to the hard drive asynchronously with the power of the new streams API. That will have support for progress, cancelation and knowing when it's done writing

FileSaver.js

FileSaver.js is the solution to saving files on the client-side, and is perfect for web apps that generates files on the client, However if the file is coming from the server we recommend you to first try to use Content-Disposition attachment response header as it has more cross-browser compatiblity.

Looking for canvas.toBlob() for saving canvases? Check out canvas-toBlob.js for a cross-browser implementation.

Supported Browsers

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 {
    var isFileSaverSupported = !!new Blob;
} catch (e) {}

IE < 10

It is possible to save text files in IE < 10 without Flash-based polyfills. See ChenWenBrian and koffsyrup's saveTextAs() for more details.

Safari 6.1+

Blobs may be opened instead of saved sometimes—you may have to direct your Safari users to manually press ⌘+S to save the file after it is opened. Using the application/octet-stream MIME type to force downloads can cause issues in Safari.

iOS

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 please tell Apple how this WebKit bug is affecting you.

Syntax

Import saveAs() from file-saver

import { saveAs } from 'file-saver';
FileSaver saveAs(Blob/File/Url, optional DOMString filename, optional Object { 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 has charset=utf-8 set.

Examples

Saving text using require()

var FileSaver = require('file-saver');
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(blob, "hello world.txt");

Saving text

var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(blob, "hello world.txt");

Saving URLs

FileSaver.saveAs("https://httpbin.org/image", "image.jpg");

Using URLs within the same origin will just use a[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 using a[download].

The standard W3C File API Blob interface is not available in all browsers. Blob.js is a cross-browser Blob implementation that solves this.

Saving a canvas

var canvas = document.getElementById("my-canvas");
canvas.toBlob(function(blob) {
    saveAs(blob, "pretty image.png");
});

Note: The standard HTML5 canvas.toBlob() method is not available in all browsers. canvas-toBlob.js is a cross-browser canvas.toBlob() that polyfills this.

Saving File

You can save a File constructor without specifying a filename. If the file itself already contains a name, there is a hand full of ways to get a file instance (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)
var file = new File(["Hello, world!"], "hello world.txt", {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(file);

Tracking image

Installation

# Basic Node.JS installation
npm install file-saver --save
bower install file-saver

Additionally, TypeScript definitions can be installed via:

# Additional typescript definitions
npm install @types/file-saver --save-dev

More Repositories

1

Blob.js

An HTML5 Blob implementation
JavaScript
1,141
star
2

classList.js

Cross-browser element.classList
JavaScript
1,105
star
3

canvas-toBlob.js

A canvas.toBlob() implementation
JavaScript
657
star
4

l10n.js

Passive localization JavaScript library
JavaScript
392
star
5

jsandbox

A JavaScript sandboxing library that uses web worker threads
JavaScript
190
star
6

color.js

Color management JavaScript libary
JavaScript
157
star
7

async-document-write

An asynchronous document.write implementation
JavaScript
65
star
8

pmxdr

Cross-domain XHR using postMessage
JavaScript
63
star
9

voice-search

Chrome extension for searching by speaking.
JavaScript
48
star
10

async.js

async/await before promises. async.js facilitates asynchronous actions ('promises') with synchronous-style syntax
JavaScript
41
star
11

timer.js

High-precision JavaScript timer
JavaScript
37
star
12

Xccessors

Xccessors (cross-browser accessors) is a JavaScript shim that implements the legacy or standard methods for defining and looking up accessors (getters and setters) of objects.
JavaScript
36
star
13

RetargetMouseScroll

A JavaScript library for retargetting mouse scroll events.
HTML
35
star
14

libxdr

A library that implements a cross-browser XDR constructor
JavaScript
27
star
15

hotlink.js

Hide image referrers
JavaScript
26
star
16

jData-host

jData host JavaScript library.
JavaScript
18
star
17

mumbl

A JavaScript library that abstracts audio-playing functionality of HTML5, Songbird, and SoundManager 2 for use in music playlists
JavaScript
17
star
18

tinylog

A minimalistic logging platform
JavaScript
17
star
19

reddit-bots

A collection of various reddit bots.
Python
17
star
20

emoji-favicon-toolkit

Emoji Favicon Toolkit - Set your favicon to emoji using canvas & cache as /favicon.ico with service workers
TypeScript
15
star
21

e4x.js

Implementation of all of the optional features in the ECMA-357 specification.
JavaScript
13
star
22

subscribe.js

An easy-to-use and highly extensible client-side notification-based feed reader Service Worker (coming soon!)
JavaScript
11
star
23

mutaprophylaxis

Methods for preventing unauthorized DOM mutations
JavaScript
10
star
24

code.eligrey.com-archive

Archive of code.eligrey.com
JavaScript
8
star
25

attrs

JavaScript 1.6+ library for setting DOM element attributes
JavaScript
7
star
26

chrome-enhanced-font-smoothing

Chrome extension that that improves font smoothing on Windows
CSS
6
star
27

CiteDrag

CiteDrag automatically cites data dragged from one website to a normal text input (ie. input type="text", textarea) or rich text input field (ie. Microsoft Word, contenteditable HTML elements, your blogging platform, etc.)
JavaScript
5
star
28

BlobBuilder.js

Moved to https://github.com/eligrey/Blob.js
4
star
29

js-iterators

A collection of iterator and generator-related JavaScript scripts.
JavaScript
4
star
30

http-index-format-automator

Generates application/http-index-format for directory listings
PHP
3
star
31

mixest-downloader

Adds download functionality to mixest.com.
JavaScript
3
star
32

jil

jData Interface Library (client JavaScript library)
JavaScript
3
star
33

myrandom.js

reddit's paywalled myrandom button, for everyone
JavaScript
2
star
34

e4x-array-methods.js

A JavaScript library that implements array methods for E4X XML. Intended for making XML easier to use with server-side JavaScript.
JavaScript
2
star
35

docswap

DOM document replacement tools
TypeScript
1
star
36

clipvertise

Use the clipboard for ads or something
1
star
37

speculative-request-control

Speculative Request Control explainer
Bikeshed
1
star