实现浏览器端生成并保存文件的 JavaScript 库 FileSaver.js 使用说明:
一、引入 JavaScript 文件:
<script src="path/FileSaver.js"/>
二、例子:
保存成文本文件
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"}); saveAs(blob, "hello world.txt");
保存成图片
var canvas = document.getElementById("my-canvas"), ctx = canvas.getContext("2d");// draw to canvas...canvas.toBlob(function(blob) { saveAs(blob, "pretty image.png"); });
支持的浏览器:
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 | 500 MiB | None |
Chrome for Android | Blob | Yes | 500 MiB | 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 |
var htmldome = '<!doctype html>' + wrap + '<html lang="en">' + wrap + '<head>' + wrap + '<meta set="utf8">' + wrap + '<meta httpequiv="xuacompatible" content="ie=edge">' + wrap + '<meta name="viewpt" content="width=devicewidth, initialscale=1">' + wrap + '<meta name="descripttion" content="">' + wrap + '<meta name="keywds" content="">' + wrap + '<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">' + wrap + '<![if lt ie 9]>' + wrap + '<scriptt src="js/html5shiv.js"></scriptt>' + wrap + '<scriptt src="js/respond.min.js"></scriptt>' + wrap + '<![endif]>' + wrap + '</head>' + wrap + '<body>' + allhtmlcontent + '</body> ' + wrap + '</html>'; var objectname = 'index.html'; var blob = new blob(htmldome, { type: "text/plain;set=utf8" }); saveas(blob, objectname);
这样无法保存吗