@elastic/filesaver

中文文档
1.1.2发布于 2017/08/09MITCommonJS零依赖近 30 天下载 10.7万
原始使用文档
文档目录10 节
  1. Supported browsers
  2. IE < 10
  3. Safari 6.1+
  4. iOS
  5. Syntax
  6. Examples
  7. Saving text
  8. Saving a canvas
  9. Contributing
  10. Bower Installation

FileSaver.js

FileSaver.js implements the HTML5 W3C saveAs() FileSaver interface in browsers that do not natively support it. There is a FileSaver.js demo that demonstrates saving various media types.

FileSaver.js is the solution to saving files on the client-side, and is perfect for webapps that need to generate files, or for saving sensitive information that shouldn't be sent to an external server.

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

Supported browsers

BrowserConstructs asFilenamesMax Blob SizeDependencies
Firefox 20+BlobYes800 MiBNone
Firefox < 20data: URINon/aBlob.js
ChromeBlobYes500 MiBNone
Chrome for AndroidBlobYes500 MiBNone
IE 10+BlobYes600 MiBNone
Opera 15+BlobYes500 MiBNone
Opera < 15data: URINon/aBlob.js
Safari 6.1+*BlobNo?None
Safari < 6data: URINon/aBlob.js

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 bug is affecting you.

Syntax

FileSaver saveAs(in Blob data, in DOMString filename)

Examples

Saving text

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

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"), ctx = canvas.getContext("2d");
// draw to 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.

Tracking image

Contributing

The FileSaver.js distribution file is compiled with Uglify.js like so:

uglifyjs FileSaver.js --comments /@source/ > FileSaver.min.js

Please make sure you build a production version before submitting a pull request.

Bower Installation

Please see the this repo for a bower-compatible fork of FileSaver.js, available under the package name file-saver.js.

版本与发布记录

@elastic/filesaver 共发布 1 个版本,首次发布于 2017/08/09;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.2latest2017/08/099 年前

关于 @elastic/filesaver 的常见问题

@elastic/filesaver 是什么?

https://github.com/eligrey/FileSaver.js

如何安装 @elastic/filesaver?

在项目目录运行 npm install @elastic/filesaver,或使用 pnpm add / yarn add / bun add。

@elastic/filesaver 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/elastic__filesaver 是否存在。

@elastic/filesaver 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@elastic/filesaver 可以商用吗?

@elastic/filesaver 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@elastic/filesaver 的下载量有多少?

根据 npm 官方下载统计,@elastic/filesaver 在 2026/10/14 – 2026/11/12 这 30 天内被下载 107,117 次,日均约 3,571 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@elastic/filesaver 还在维护吗?

最新版本 1.1.2 发布于 2017/08/09(9 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/08/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。