Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

An HTML5 saveAs() FileSaver implementation

License

NotificationsYou must be signed in to change notification settings

eligrey/FileSaver.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

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.

Supported Browsers

BrowserConstructs asFilenamesMax Blob SizeDependencies
Firefox 20+BlobYes800 MiBNone
Firefox < 20data: URINon/aBlob.js
ChromeBlobYes2GBNone
Chrome for AndroidBlobYesRAM/5None
EdgeBlobYes?None
IE 10+BlobYes600 MiBNone
Opera 15+BlobYes500 MiBNone
Opera < 15data: URINon/aBlob.js
Safari 6.1+*BlobNo?None
Safari < 6data: URINon/aBlob.js
Safari 10.1+  Blob        Yes        n/a          None

Feature detection is possible:

try{varisFileSaverSupported=!!newBlob;}catch(e){}

IE < 10

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

Safari 6.1+

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.

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

Syntax

ImportsaveAs() from file-saver

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.

Examples

Saving text usingrequire()

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

Saving text

varblob=newBlob(["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 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.

Saving a canvas

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.

Saving File

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);

Tracking image

Installation

# 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

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp