60 Commits
Author SHA1 Message Date
Eli Grey e865e37af9 Revert #408 2018-03-22 14:09:13 -07:00
Christopher HuntandJimmy Wärting a606064f37 Revert interface change. (#411) 2018-03-22 19:48:05 +01:00
Bryan WainandJimmy Wärting 28a86d90c4 Fix files included with npm (#413)
the files pointed to by main and module are not included in the npm installation
2018-03-22 19:47:45 +01:00
Eli Grey 5914e2a573 Version bump for 1.3.7 2018-03-20 14:30:18 -07:00
Eli Grey 6356c3f6f6 Set target="_blank" on save link
Also normalize setImmediate usage

Fixes #408
2018-03-16 14:45:20 -07:00
Eli GreyandGitHub 24f9aaca80 Merge pull request #397 from Rillke/patch-1
Update @source in FileSaver.js
2018-01-25 01:46:34 -08:00
Rainer RillkeandGitHub f1959c87cd Update @source in FileSaver.js 2018-01-25 01:54:51 +01:00
Eli GreyandGitHub dfd58561fb Use export default 2018-01-22 15:53:17 -08:00
Eli GreyandGitHub a5dbf55c17 Merge pull request #391 from ogonkov/use_babel
Run build via `babel`. More changes and cleanup to come
2018-01-22 14:49:06 -08:00
Alexey 5978f3c577 Avoid using window in export 2018-01-23 00:12:19 +03:00
Alexey 9570cf7362 Update readme 2018-01-14 08:50:55 +00:00
Alexey b1158d0c33 Update main in bower.json 2018-01-14 08:44:50 +00:00
Alexey b6c236d96d Add check for window.saveAs 2018-01-14 08:43:44 +00:00
Alexey 164deec5ab Build to dist folder 2018-01-13 09:01:32 +00:00
Alexey 99a89750b0 Update prepublish script name 2018-01-13 08:39:48 +00:00
Alexey ff675f8052 Change to named export 2018-01-13 08:37:02 +00:00
Alexey 17d5c67eb3 Fix files section in package.json 2018-01-13 08:37:02 +00:00
Alexey 70bb521e5d Preserve generated files location 2018-01-13 08:37:02 +00:00
Alexey b4c11367c0 Build minified version 2018-01-13 08:37:02 +00:00
Alexey 281a1c1a93 Run build before publishing 2018-01-13 08:37:02 +00:00
Alexey d02373e40d Preserve source file for npm 2018-01-13 08:37:02 +00:00
Alexey d7f5db3244 Run build via babel 2018-01-13 08:37:02 +00:00
Eli Grey b4a918669a Update version number for #387 2018-01-12 05:03:49 -08:00
Eli Grey 5733e40e5a Fixes #387 2018-01-12 05:00:59 -08:00
Eli GreyandGitHub 74f089808e Merge pull request #387 from arasabbasi/patch-1
Fix RollUp-Error because of This is Undefined Rewrite
2018-01-12 04:56:18 -08:00
Aras AbbasiandGitHub fe42ec4055 Update FileSaver.js 2018-01-12 13:46:49 +01:00
Aras AbbasiandGitHub 0b90542535 Update FileSaver.js 2018-01-12 13:14:59 +01:00
JoshuaandJimmy Wärting c3e4a45021 Fix typo (#378)
Fix typo
2017-12-06 12:47:35 +01:00
Aras AbbasiandGitHub db089ce81e Update FileSaver.js 2017-12-04 19:42:44 +01:00
Aras AbbasiandGitHub 1bf9b35b54 Fix RollUp-Error because of This is Undefined Rewrite
make it compatible with RollUp
rollup/rollup#759
MrRio/jsPDF#1533
2017-12-04 19:40:44 +01:00
Eli GreyandGitHub c347c51f6e Merge pull request #374 from thijstriemstra/patch-1
fix typo
2017-09-29 17:53:01 -07:00
Thijs TriemstraandGitHub 945f08bc74 fix typo 2017-09-30 02:10:23 +02:00
Jimmy Karl Roland WärtingandGitHub dffa3a4989 Updated browser support
closes #12
2017-06-22 09:01:12 +02:00
Jimmy Karl Roland WärtingandGitHub 4db4a78aad Merge pull request #310 from marvinscharle/master
Include informations for how to get Typescript definitions into README.md
2017-03-09 16:01:41 +01:00
Marvin Scharle d89ed13171 Updated README to include information about typescript definitions eligrey/FileSaver.js#277 2017-02-15 17:24:53 +01:00
Jimmy Karl Roland WärtingandGitHub aa9f4e0e03 Merge pull request #273 from Amitesh/require-syntax-usage
Saving text using with require statement
2016-10-14 10:54:05 +02:00
Amitesh KumarandGitHub cafa1bfebb Saving text using with require statement 2016-10-14 12:09:50 +05:30
Jimmy Wärting 5ed507ef8a constructor test in safari 10 has nuked 2016-10-05 12:47:35 +02:00
Eli Grey eb8d76f845 Closes #257 2016-09-01 15:09:41 -07:00
Eli GreyandGitHub 8739c467c6 Merge pull request #159 from zky829/patch-1
Add safe module name to avoid requireJS errors
2016-08-29 15:01:25 -07:00
zky829 d8870ded68 Resolve conflicts 2016-08-28 19:42:48 +01:00
Jimmy Wärting 9751591281 new release 2016-06-16 20:26:36 +02:00
Jimmy Karl Roland WärtingandGitHub b3cad8efea Merge pull request #240 from EnigmaBridge/chromeIosSaveFix
Workaround for #179 - use datalink for Chrome iOS
2016-06-16 20:21:31 +02:00
Dusan Klinec 3843680045 Workaround for #179 - use datalink for Chrome iOS
- Blob links / url objects are not supported in Chrome iOS for now
- thx @jimmywarting for suggestions & help with improving this PR
2016-06-16 09:04:45 +02:00
Jimmy Wärting 4ac2c6d6c2 Fixed min version 2016-06-06 23:09:13 +02:00
Jimmy Wärting f1a8bf9fb0 Better to use text/plain for safari so it can be rendered and then be saved 2016-06-03 17:08:08 +02:00
Jimmy Karl Roland Wärting b2bf6d86f5 typo 2016-06-03 11:40:32 +02:00
Eli Grey 0b137cf80a Merge pull request #236 from eligrey/jimmywarting-patch-1
Public relations (PR) for StreamSaver.js
2016-06-03 05:19:55 -04:00
Jimmy Karl Roland Wärting d84a5c2f3a Public relations (PR) for StreamSaver.js 2016-06-03 11:07:17 +02:00
Jimmy Karl Roland Wärting 47ff4cd74a moved some text 2016-05-31 09:46:57 +02:00
Jimmy Karl Roland Wärting c0c85f387b moved some text 2016-05-31 09:46:11 +02:00
Jimmy Wärting 3eeddf1145 1.3.0 2016-05-31 09:30:32 +02:00
Jimmy Karl Roland Wärting bc0ba24aaa Added failsafe
Closes #216
2016-05-31 09:15:12 +02:00
Jimmy Karl Roland Wärting ae387dbbac Merge pull request #233 from eligrey/remove_filesystem
Removed filesystem and support saving with file name
2016-05-31 09:12:32 +02:00
Jimmy Karl Roland Wärting b610731175 Merge pull request #234 from sjurba/patch-1
Don't ignore the minified file in bower.json
2016-05-30 15:39:07 +02:00
Sjur Bakka 9aee871752 Don't ignore the minified file 2016-05-30 15:36:36 +02:00
Jimmy Wärting 1bb6b90443 closes #174
fixs #231
2016-05-27 23:25:24 +02:00
Jimmy Wärting a5a0ae83db updated versions 2016-05-27 09:31:19 +02:00
Eli Grey 8e64e56f47 Workaround for #230 2016-05-26 22:04:32 -04:00
z.ky 7b242c2d78 Add safe module name to avoid requireJS errors
If a safe module name is not defined for requireJS, it will throw a "Mismatched anonymous define() modules..." error in the browser.
2015-07-15 14:23:01 +01:00
12 changed files with 3047 additions and 307 deletions
+19
View File
@@ -0,0 +1,19 @@
{
"presets": [
["env", {
"targets": {
"browsers": [
"ie > 9"
]
}
}]
],
"plugins": [
"transform-es2015-modules-umd"
],
"env": {
"production": {
"presets": ["minify"]
}
}
}
+6
View File
@@ -0,0 +1,6 @@
node_modules/
.DS_Store
.idea/
# Generated files
/dist/
-281
View File
@@ -1,281 +0,0 @@
/* FileSaver.js
* A saveAs() FileSaver implementation.
* 1.2.1
*
* By Eli Grey, http://eligrey.com
* License: MIT
* See https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md
*/
/*global self */
/*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */
/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
var saveAs = saveAs || (function(view) {
"use strict";
// IE <10 is explicitly unsupported
if (typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) {
return;
}
var
doc = view.document
// only get URL when necessary in case Blob.js hasn't overridden it yet
, get_URL = function() {
return view.URL || view.webkitURL || view;
}
, save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a")
, can_use_save_link = "download" in save_link
, click = function(node) {
var event = new MouseEvent("click");
node.dispatchEvent(event);
}
, is_safari = /Version\/[\d\.]+.*Safari/.test(navigator.userAgent)
, webkit_req_fs = view.webkitRequestFileSystem
, req_fs = view.requestFileSystem || webkit_req_fs || view.mozRequestFileSystem
, throw_outside = function(ex) {
(view.setImmediate || view.setTimeout)(function() {
throw ex;
}, 0);
}
, force_saveable_type = "application/octet-stream"
, fs_min_size = 0
// the Blob API is fundamentally broken as there is no "downloadfinished" event to subscribe to
, arbitrary_revoke_timeout = 1000 * 40 // in ms
, revoke = function(file) {
var revoker = function() {
if (typeof file === "string") { // file is an object URL
get_URL().revokeObjectURL(file);
} else { // file is a File
file.remove();
}
};
/* // Take note W3C:
var
uri = typeof file === "string" ? file : file.toURL()
, revoker = function(evt) {
// idealy DownloadFinishedEvent.data would be the URL requested
if (evt.data === uri) {
if (typeof file === "string") { // file is an object URL
get_URL().revokeObjectURL(file);
} else { // file is a File
file.remove();
}
}
}
;
view.addEventListener("downloadfinished", revoker);
*/
setTimeout(revoker, arbitrary_revoke_timeout);
}
, dispatch = function(filesaver, event_types, event) {
event_types = [].concat(event_types);
var i = event_types.length;
while (i--) {
var listener = filesaver["on" + event_types[i]];
if (typeof listener === "function") {
try {
listener.call(filesaver, event || filesaver);
} catch (ex) {
throw_outside(ex);
}
}
}
}
, auto_bom = function(blob) {
// prepend BOM for UTF-8 XML and text/* types (including HTML)
// note: your browser will automatically convert UTF-16 \uFEFF to EF BB BF
if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
return new Blob(["\uFEFF", blob], {type: blob.type});
}
return blob;
}
, FileSaver = function(blob, name, no_auto_bom) {
if (!no_auto_bom) {
blob = auto_bom(blob);
}
// First try a.download, then web filesystem, then object URLs
var
filesaver = this
, type = blob.type
, blob_changed = false
, object_url
, target_view
, dispatch_all = function() {
dispatch(filesaver, "writestart progress write writeend".split(" "));
}
// on any filesys errors revert to saving with object URLs
, fs_error = function() {
if (target_view && is_safari && typeof FileReader !== "undefined") {
// Safari doesn't allow downloading of blob urls
var reader = new FileReader();
reader.onloadend = function() {
var base64Data = reader.result;
target_view.location.href = "data:attachment/file" + base64Data.slice(base64Data.search(/[,;]/));
filesaver.readyState = filesaver.DONE;
dispatch_all();
};
reader.readAsDataURL(blob);
filesaver.readyState = filesaver.INIT;
return;
}
// don't create more object URLs than needed
if (blob_changed || !object_url) {
object_url = get_URL().createObjectURL(blob);
}
if (target_view) {
target_view.location.href = object_url;
} else {
var new_tab = view.open(object_url, "_blank");
if (new_tab === undefined && is_safari) {
// Apple does not allow window.open, see https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/WorkingwithWindowsandTabs/WorkingwithWindowsandTabs.html
view.location.href = object_url;
}
}
filesaver.readyState = filesaver.DONE;
dispatch_all();
revoke(object_url);
}
, abortable = function(func) {
return function() {
if (filesaver.readyState !== filesaver.DONE) {
return func.apply(this, arguments);
}
};
}
, create_if_not_found = {create: true, exclusive: false}
, slice
;
filesaver.readyState = filesaver.INIT;
if (!name) {
name = "download";
}
if (can_use_save_link) {
object_url = get_URL().createObjectURL(blob);
setTimeout(function() {
save_link.href = object_url;
save_link.download = name;
click(save_link);
dispatch_all();
revoke(object_url);
filesaver.readyState = filesaver.DONE;
});
return;
}
// Object and web filesystem URLs have a problem saving in Google Chrome when
// viewed in a tab, so I force save with application/octet-stream
// http://code.google.com/p/chromium/issues/detail?id=91158
// Update: Google errantly closed 91158, I submitted it again:
// https://code.google.com/p/chromium/issues/detail?id=389642
if (view.chrome && type && type !== force_saveable_type) {
slice = blob.slice || blob.webkitSlice;
blob = slice.call(blob, 0, blob.size, force_saveable_type);
blob_changed = true;
}
// Since I can't be sure that the guessed media type will trigger a download
// in WebKit, I append .download to the filename.
// https://bugs.webkit.org/show_bug.cgi?id=65440
if (webkit_req_fs && name !== "download") {
name += ".download";
}
if (type === force_saveable_type || webkit_req_fs) {
target_view = view;
}
if (!req_fs) {
fs_error();
return;
}
fs_min_size += blob.size;
req_fs(view.TEMPORARY, fs_min_size, abortable(function(fs) {
fs.root.getDirectory("saved", create_if_not_found, abortable(function(dir) {
var save = function() {
dir.getFile(name, create_if_not_found, abortable(function(file) {
file.createWriter(abortable(function(writer) {
writer.onwriteend = function(event) {
target_view.location.href = file.toURL();
filesaver.readyState = filesaver.DONE;
dispatch(filesaver, "writeend", event);
revoke(file);
};
writer.onerror = function() {
var error = writer.error;
if (error.code !== error.ABORT_ERR) {
fs_error();
}
};
"writestart progress write abort".split(" ").forEach(function(event) {
writer["on" + event] = filesaver["on" + event];
});
writer.write(blob);
filesaver.abort = function() {
writer.abort();
filesaver.readyState = filesaver.DONE;
};
filesaver.readyState = filesaver.WRITING;
}), fs_error);
}), fs_error);
};
dir.getFile(name, {create: false}, abortable(function(file) {
// delete file if it already exists
file.remove();
save();
}), abortable(function(ex) {
if (ex.code === ex.NOT_FOUND_ERR) {
save();
} else {
fs_error();
}
}));
}), fs_error);
}), fs_error);
}
, FS_proto = FileSaver.prototype
, saveAs = function(blob, name, no_auto_bom) {
return new FileSaver(blob, name, no_auto_bom);
}
;
// IE 10+ (native saveAs)
if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) {
return function(blob, name, no_auto_bom) {
if (!no_auto_bom) {
blob = auto_bom(blob);
}
return navigator.msSaveOrOpenBlob(blob, name || "download");
};
}
FS_proto.abort = function() {
var filesaver = this;
filesaver.readyState = filesaver.DONE;
dispatch(filesaver, "abort");
};
FS_proto.readyState = FS_proto.INIT = 0;
FS_proto.WRITING = 1;
FS_proto.DONE = 2;
FS_proto.error =
FS_proto.onwritestart =
FS_proto.onprogress =
FS_proto.onwrite =
FS_proto.onabort =
FS_proto.onerror =
FS_proto.onwriteend =
null;
return saveAs;
}(
typeof self !== "undefined" && self
|| typeof window !== "undefined" && window
|| this.content
));
// `self` is undefined in Firefox for Android content script context
// while `this` is nsIContentFrameMessageManager
// with an attribute `content` that corresponds to the window
if (typeof module !== "undefined" && module.exports) {
module.exports.saveAs = saveAs;
} else if ((typeof define !== "undefined" && define !== null) && (define.amd !== null)) {
define([], function() {
return saveAs;
});
}
-2
View File
@@ -1,2 +0,0 @@
/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
var saveAs=saveAs||function(e){"use strict";if("undefined"==typeof navigator||!/MSIE [1-9]\./.test(navigator.userAgent)){var t=e.document,n=function(){return e.URL||e.webkitURL||e},o=t.createElementNS("http://www.w3.org/1999/xhtml","a"),r="download"in o,i=function(e){var t=new MouseEvent("click");e.dispatchEvent(t)},a=/Version\/[\d\.]+.*Safari/.test(navigator.userAgent),c=e.webkitRequestFileSystem,d=e.requestFileSystem||c||e.mozRequestFileSystem,u=function(t){(e.setImmediate||e.setTimeout)(function(){throw t},0)},s="application/octet-stream",f=0,l=4e4,v=function(e){var t=function(){"string"==typeof e?n().revokeObjectURL(e):e.remove()};setTimeout(t,l)},p=function(e,t,n){t=[].concat(t);for(var o=t.length;o--;){var r=e["on"+t[o]];if("function"==typeof r)try{r.call(e,n||e)}catch(i){u(i)}}},w=function(e){return/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(e.type)?new Blob(["\uFEFF",e],{type:e.type}):e},y=function(t,u,l){l||(t=w(t));var y,m,S,h=this,R=t.type,O=!1,g=function(){p(h,"writestart progress write writeend".split(" "))},b=function(){if(m&&a&&"undefined"!=typeof FileReader){var o=new FileReader;return o.onloadend=function(){var e=o.result;m.location.href="data:attachment/file"+e.slice(e.search(/[,;]/)),h.readyState=h.DONE,g()},o.readAsDataURL(t),void(h.readyState=h.INIT)}if((O||!y)&&(y=n().createObjectURL(t)),m)m.location.href=y;else{var r=e.open(y,"_blank");void 0===r&&a&&(e.location.href=y)}h.readyState=h.DONE,g(),v(y)},E=function(e){return function(){return h.readyState!==h.DONE?e.apply(this,arguments):void 0}},N={create:!0,exclusive:!1};return h.readyState=h.INIT,u||(u="download"),r?(y=n().createObjectURL(t),void setTimeout(function(){o.href=y,o.download=u,i(o),g(),v(y),h.readyState=h.DONE})):(e.chrome&&R&&R!==s&&(S=t.slice||t.webkitSlice,t=S.call(t,0,t.size,s),O=!0),c&&"download"!==u&&(u+=".download"),(R===s||c)&&(m=e),d?(f+=t.size,void d(e.TEMPORARY,f,E(function(e){e.root.getDirectory("saved",N,E(function(e){var n=function(){e.getFile(u,N,E(function(e){e.createWriter(E(function(n){n.onwriteend=function(t){m.location.href=e.toURL(),h.readyState=h.DONE,p(h,"writeend",t),v(e)},n.onerror=function(){var e=n.error;e.code!==e.ABORT_ERR&&b()},"writestart progress write abort".split(" ").forEach(function(e){n["on"+e]=h["on"+e]}),n.write(t),h.abort=function(){n.abort(),h.readyState=h.DONE},h.readyState=h.WRITING}),b)}),b)};e.getFile(u,{create:!1},E(function(e){e.remove(),n()}),E(function(e){e.code===e.NOT_FOUND_ERR?n():b()}))}),b)}),b)):void b())},m=y.prototype,S=function(e,t,n){return new y(e,t,n)};return"undefined"!=typeof navigator&&navigator.msSaveOrOpenBlob?function(e,t,n){return n||(e=w(e)),navigator.msSaveOrOpenBlob(e,t||"download")}:(m.abort=function(){var e=this;e.readyState=e.DONE,p(e,"abort")},m.readyState=m.INIT=0,m.WRITING=1,m.DONE=2,m.error=m.onwritestart=m.onprogress=m.onwrite=m.onabort=m.onerror=m.onwriteend=null,S)}}("undefined"!=typeof self&&self||"undefined"!=typeof window&&window||this.content);"undefined"!=typeof module&&module.exports?module.exports.saveAs=saveAs:"undefined"!=typeof define&&null!==define&&null!==define.amd&&define([],function(){return saveAs});
+2
View File
@@ -1,3 +1,5 @@
The MIT License
Copyright © 2016 [Eli Grey][1]. Copyright © 2016 [Eli Grey][1].
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+34 -15
View File
@@ -1,3 +1,8 @@
If you need to save really large files bigger then the blob's size limitation or don't have
enough RAM, then have a look at the more advanced [StreamSaver.js](https://github.com/jimmywarting/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
============ ============
@@ -27,6 +32,7 @@ Supported browsers
| Opera < 15 | data: URI | No | n/a | [Blob.js](https://github.com/eligrey/Blob.js) | | Opera < 15 | data: URI | No | n/a | [Blob.js](https://github.com/eligrey/Blob.js) |
| Safari 6.1+* | Blob | No | ? | None | | Safari 6.1+* | Blob | No | ? | None |
| Safari < 6 | data: URI | No | n/a | [Blob.js](https://github.com/eligrey/Blob.js) | | Safari < 6 | data: URI | No | n/a | [Blob.js](https://github.com/eligrey/Blob.js) |
| Safari 10.1+   | Blob         | Yes         | n/a           | None |
Feature detection is possible: Feature detection is possible:
@@ -54,7 +60,7 @@ Syntax
------ ------
```js ```js
FileSaver saveAs(Blob data, DOMString filename, optional Boolean disableAutoBOM) FileSaver saveAs(Blob/File data, optional DOMString filename, optional Boolean disableAutoBOM)
``` ```
Pass `true` for `disableAutoBOM` if you don't want FileSaver.js to automatically provide Unicode text encoding hints (see: [byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark)). Pass `true` for `disableAutoBOM` if you don't want FileSaver.js to automatically provide Unicode text encoding hints (see: [byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark)).
@@ -62,11 +68,18 @@ Pass `true` for `disableAutoBOM` if you don't want FileSaver.js to automatically
Examples Examples
-------- --------
### Saving text using require
```js
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 ### Saving text
```js ```js
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"}); var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
saveAs(blob, "hello world.txt"); FileSaver.saveAs(blob, "hello world.txt");
``` ```
The standard W3C File API [`Blob`][4] interface is not available in all browsers. The standard W3C File API [`Blob`][4] interface is not available in all browsers.
@@ -85,6 +98,19 @@ canvas.toBlob(function(blob) {
Note: The standard HTML5 `canvas.toBlob()` method is not available in all browsers. Note: The standard HTML5 `canvas.toBlob()` method is not available in all browsers.
[canvas-toBlob.js][6] is a cross-browser `canvas.toBlob()` that polyfills this. [canvas-toBlob.js][6] is a cross-browser `canvas.toBlob()` that polyfills this.
### Saving File
You can save a File constructor without specifying a filename. 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)
But if you still want to change the name, then you can change it in the 2nd argument
```js
var file = new File(["Hello, world!"], "hello world.txt", {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(file);
```
![Tracking image](https://in.getclicky.com/212712ns.gif) ![Tracking image](https://in.getclicky.com/212712ns.gif)
@@ -95,19 +121,6 @@ Note: The standard HTML5 `canvas.toBlob()` method is not available in all browse
[5]: https://github.com/eligrey/Blob.js [5]: https://github.com/eligrey/Blob.js
[6]: https://github.com/eligrey/canvas-toBlob.js [6]: https://github.com/eligrey/canvas-toBlob.js
Contributing
------------
The `FileSaver.js` distribution file is compiled with Uglify.js like so:
```bash
uglifyjs FileSaver.js --mangle --comments /@source/ > FileSaver.min.js
# or simply:
npm run build
```
Please make sure you build a production version before submitting a pull request.
Installation Installation
------------------ ------------------
@@ -115,3 +128,9 @@ Installation
npm install file-saver --save npm install file-saver --save
bower install file-saver bower install file-saver
``` ```
Additionally, TypeScript definitions can be installed via:
```bash
npm install @types/file-saver --save-dev
```
+3 -3
View File
@@ -1,7 +1,7 @@
{ {
"name": "file-saver", "name": "file-saver",
"main": "FileSaver.js", "main": "dist/FileSaver.js",
"version": "1.2.0", "version": "1.3.8",
"homepage": "https://github.com/eligrey/FileSaver.js", "homepage": "https://github.com/eligrey/FileSaver.js",
"authors": [ "authors": [
"Eli Grey <[email protected]>" "Eli Grey <[email protected]>"
@@ -15,7 +15,7 @@
"license": "MIT", "license": "MIT",
"ignore": [ "ignore": [
"*", "*",
"!FileSaver.js", "!FileSaver.*js",
"!LICENSE.md" "!LICENSE.md"
] ]
} }
Regular → Executable
+1 -1
View File
@@ -195,7 +195,7 @@ html_options_form.addEventListener("submit", function(event) {
saveAs( saveAs(
new BB( new BB(
[xml_serializer.serializeToString(doc)] [xml_serializer.serializeToString(doc)]
, {type: "application/xhtml+xml;charset=" + document.characterSet} , {type: "text/plain;charset=" + document.characterSet}
) )
, (html_filename.value || html_filename.placeholder) + ".xhtml" , (html_filename.value || html_filename.placeholder) + ".xhtml"
); );
Vendored Regular → Executable
+1 -1
View File
@@ -1,2 +1,2 @@
/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/demo/demo.js */ /*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/demo/demo.js */
(function(n){"use strict";var s=n.document,g=function(A){return s.getElementById(A)},b=n.sessionStorage,x=function(){return n.Blob},f=g("canvas"),r=g("canvas-options"),y=g("canvas-filename"),p=g("canvas-clear"),q=g("text"),t=g("text-options"),h=g("text-filename"),m=g("html"),e=g("html-options"),i=g("html-filename"),u=f.getContext("2d"),z=false,a=b.x_points||[],o=b.y_points||[],d=b.drag_points||[],j=function(A,C,B){a.push(A);o.push(C);d.push(B)},l=function(){f.width=f.width;u.lineWidth=6;u.lineJoin="round";u.strokeStyle="#000000";var B=0,A=a.length;for(;B<A;B++){u.beginPath();if(B&&d[B]){u.moveTo(a[B-1],o[B-1])}else{u.moveTo(a[B]-1,o[B])}u.lineTo(a[B],o[B]);u.closePath();u.stroke()}},c=function(){z=false},w=function(E){var D="h6 h5 h4 h3 h2 h1".split(" "),C=D.length,F,G;while(C--){F=E.getElementsByTagName(D[C]);for(var B=0,A=F.length;B<A;B++){G=F[B].textContent.trim();if(G){return G}}}},v=s.implementation,k=function(D){var B=v.createDocumentType("html",null,null),J=v.createDocument("http://www.w3.org/1999/xhtml","html",B),A=J.documentElement,H=A.appendChild(J.createElement("head")),K=H.appendChild(J.createElement("meta")),I=H.appendChild(J.createElement("title")),E=A.appendChild(J.createElement("body")),C=0,G=D.childNodes.length;K.setAttribute("charset",D.ownerDocument.characterSet);for(;C<G;C++){E.appendChild(J.importNode(D.childNodes.item(C),true))}var F=w(J);if(F){I.appendChild(J.createTextNode(F))}return J};f.width=500;f.height=300;if(typeof a==="string"){a=JSON.parse(a)}if(typeof o==="string"){o=JSON.parse(o)}if(typeof d==="string"){d=JSON.parse(d)}if(b.canvas_filename){y.value=b.canvas_filename}if(b.text){q.value=b.text}if(b.text_filename){h.value=b.text_filename}if(b.html){m.innerHTML=b.html}if(b.html_filename){i.value=b.html_filename}z=true;l();z=false;p.addEventListener("click",function(){f.width=f.width;a.length=o.length=d.length=0},false);f.addEventListener("mousedown",function(A){A.preventDefault();z=true;j(A.pageX-f.offsetLeft,A.pageY-f.offsetTop,false);l()},false);f.addEventListener("mousemove",function(A){if(z){j(A.pageX-f.offsetLeft,A.pageY-f.offsetTop,true);l()}},false);f.addEventListener("mouseup",c,false);f.addEventListener("mouseout",c,false);r.addEventListener("submit",function(A){A.preventDefault();f.toBlobHD(function(B){saveAs(B,(y.value||y.placeholder)+".png")},"image/png")},false);t.addEventListener("submit",function(A){A.preventDefault();var B=x();saveAs(new B([q.value||q.placeholder],{type:"text/plain;charset="+s.characterSet}),(h.value||h.placeholder)+".txt")},false);e.addEventListener("submit",function(B){B.preventDefault();var D=x(),A=new XMLSerializer,C=k(m);saveAs(new D([A.serializeToString(C)],{type:"application/xhtml+xml;charset="+s.characterSet}),(i.value||i.placeholder)+".xhtml")},false);n.addEventListener("unload",function(){b.x_points=JSON.stringify(a);b.y_points=JSON.stringify(o);b.drag_points=JSON.stringify(d);b.canvas_filename=y.value;b.text=q.value;b.text_filename=h.value;b.html=m.innerHTML;b.html_filename=i.value},false)}(self)); (function(n){"use strict";var s=n.document,g=function(A){return s.getElementById(A)},b=n.sessionStorage,x=function(){return n.Blob},f=g("canvas"),r=g("canvas-options"),y=g("canvas-filename"),p=g("canvas-clear"),q=g("text"),t=g("text-options"),h=g("text-filename"),m=g("html"),e=g("html-options"),i=g("html-filename"),u=f.getContext("2d"),z=false,a=b.x_points||[],o=b.y_points||[],d=b.drag_points||[],j=function(A,C,B){a.push(A);o.push(C);d.push(B)},l=function(){f.width=f.width;u.lineWidth=6;u.lineJoin="round";u.strokeStyle="#000000";var B=0,A=a.length;for(;B<A;B++){u.beginPath();if(B&&d[B]){u.moveTo(a[B-1],o[B-1])}else{u.moveTo(a[B]-1,o[B])}u.lineTo(a[B],o[B]);u.closePath();u.stroke()}},c=function(){z=false},w=function(E){var D="h6 h5 h4 h3 h2 h1".split(" "),C=D.length,F,G;while(C--){F=E.getElementsByTagName(D[C]);for(var B=0,A=F.length;B<A;B++){G=F[B].textContent.trim();if(G){return G}}}},v=s.implementation,k=function(D){var B=v.createDocumentType("html",null,null),J=v.createDocument("http://www.w3.org/1999/xhtml","html",B),A=J.documentElement,H=A.appendChild(J.createElement("head")),K=H.appendChild(J.createElement("meta")),I=H.appendChild(J.createElement("title")),E=A.appendChild(J.createElement("body")),C=0,G=D.childNodes.length;K.setAttribute("charset",D.ownerDocument.characterSet);for(;C<G;C++){E.appendChild(J.importNode(D.childNodes.item(C),true))}var F=w(J);if(F){I.appendChild(J.createTextNode(F))}return J};f.width=500;f.height=300;if(typeof a==="string"){a=JSON.parse(a)}if(typeof o==="string"){o=JSON.parse(o)}if(typeof d==="string"){d=JSON.parse(d)}if(b.canvas_filename){y.value=b.canvas_filename}if(b.text){q.value=b.text}if(b.text_filename){h.value=b.text_filename}if(b.html){m.innerHTML=b.html}if(b.html_filename){i.value=b.html_filename}z=true;l();z=false;p.addEventListener("click",function(){f.width=f.width;a.length=o.length=d.length=0},false);f.addEventListener("mousedown",function(A){A.preventDefault();z=true;j(A.pageX-f.offsetLeft,A.pageY-f.offsetTop,false);l()},false);f.addEventListener("mousemove",function(A){if(z){j(A.pageX-f.offsetLeft,A.pageY-f.offsetTop,true);l()}},false);f.addEventListener("mouseup",c,false);f.addEventListener("mouseout",c,false);r.addEventListener("submit",function(A){A.preventDefault();f.toBlobHD(function(B){saveAs(B,(y.value||y.placeholder)+".png")},"image/png")},false);t.addEventListener("submit",function(A){A.preventDefault();var B=x();saveAs(new B([q.value||q.placeholder],{type:"text/plain;charset="+s.characterSet}),(h.value||h.placeholder)+".txt")},false);e.addEventListener("submit",function(B){B.preventDefault();var D=x(),A=new XMLSerializer,C=k(m);saveAs(new D([A.serializeToString(C)],{type:"text/plain;charset="+s.characterSet}),(i.value||i.placeholder)+".xhtml")},false);n.addEventListener("unload",function(){b.x_points=JSON.stringify(a);b.y_points=JSON.stringify(o);b.drag_points=JSON.stringify(d);b.canvas_filename=y.value;b.text=q.value;b.text_filename=h.value;b.html=m.innerHTML;b.html_filename=i.value},false)}(self));
+2783
View File
File diff suppressed because it is too large Load Diff
+16 -4
View File
@@ -1,11 +1,20 @@
{ {
"name": "file-saver", "name": "file-saver",
"version": "1.2.0", "version": "1.3.8",
"description": "An HTML5 saveAs() FileSaver implementation", "description": "An HTML5 saveAs() FileSaver implementation",
"main": "FileSaver.js", "main": "dist/FileSaver.js",
"module": "src/FileSaver.js",
"files": [
"dist/FileSaver.js",
"dist/FileSaver.min.js",
"src/FileSaver.js"
],
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 0", "test": "echo \"Error: no test specified\" && exit 0",
"build": "node_modules/.bin/uglifyjs FileSaver.js --mangle --comments /@source/ > FileSaver.min.js" "build:development": "mkdir -p dist && babel src/FileSaver.js --out-file=dist/FileSaver.js",
"build:production": "NODE_ENV=production npm run build:development -- --out-file=dist/FileSaver.min.js",
"build": "npm run build:development && npm run build:production",
"prepublishOnly": "npm run build"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@@ -23,6 +32,9 @@
}, },
"homepage": "https://github.com/eligrey/FileSaver.js#readme", "homepage": "https://github.com/eligrey/FileSaver.js#readme",
"devDependencies": { "devDependencies": {
"uglify-js": "^2.6.2" "babel-cli": "^6.26.0",
"babel-plugin-transform-es2015-modules-umd": "^6.24.1",
"babel-preset-env": "^1.6.1",
"babel-preset-minify": "^0.2.0"
} }
} }
+182
View File
@@ -0,0 +1,182 @@
/* FileSaver.js
* A saveAs() FileSaver implementation.
* 1.3.8
* 2018-03-22 14:03:47
*
* By Eli Grey, https://eligrey.com
* License: MIT
* See https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md
*/
/*global self */
/*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */
/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/src/FileSaver.js */
export var saveAs = saveAs || (function(view) {
"use strict";
// IE <10 is explicitly unsupported
if (typeof view === "undefined" || typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) {
return;
}
var
doc = view.document
// only get URL when necessary in case Blob.js hasn't overridden it yet
, get_URL = function() {
return view.URL || view.webkitURL || view;
}
, save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a")
, can_use_save_link = "download" in save_link
, click = function(node) {
var event = new MouseEvent("click");
node.dispatchEvent(event);
}
, is_safari = /constructor/i.test(view.HTMLElement) || view.safari
, is_chrome_ios =/CriOS\/[\d]+/.test(navigator.userAgent)
, setImmediate = view.setImmediate || view.setTimeout
, throw_outside = function(ex) {
setImmediate(function() {
throw ex;
}, 0);
}
, force_saveable_type = "application/octet-stream"
// the Blob API is fundamentally broken as there is no "downloadfinished" event to subscribe to
, arbitrary_revoke_timeout = 1000 * 40 // in ms
, revoke = function(file) {
var revoker = function() {
if (typeof file === "string") { // file is an object URL
get_URL().revokeObjectURL(file);
} else { // file is a File
file.remove();
}
};
setTimeout(revoker, arbitrary_revoke_timeout);
}
, dispatch = function(filesaver, event_types, event) {
event_types = [].concat(event_types);
var i = event_types.length;
while (i--) {
var listener = filesaver["on" + event_types[i]];
if (typeof listener === "function") {
try {
listener.call(filesaver, event || filesaver);
} catch (ex) {
throw_outside(ex);
}
}
}
}
, auto_bom = function(blob) {
// prepend BOM for UTF-8 XML and text/* types (including HTML)
// note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
return new Blob([String.fromCharCode(0xFEFF), blob], {type: blob.type});
}
return blob;
}
, FileSaver = function(blob, name, no_auto_bom) {
if (!no_auto_bom) {
blob = auto_bom(blob);
}
// First try a.download, then web filesystem, then object URLs
var
filesaver = this
, type = blob.type
, force = type === force_saveable_type
, object_url
, dispatch_all = function() {
dispatch(filesaver, "writestart progress write writeend".split(" "));
}
// on any filesys errors revert to saving with object URLs
, fs_error = function() {
if ((is_chrome_ios || (force && is_safari)) && view.FileReader) {
// Safari doesn't allow downloading of blob urls
var reader = new FileReader();
reader.onloadend = function() {
var url = is_chrome_ios ? reader.result : reader.result.replace(/^data:[^;]*;/, 'data:attachment/file;');
var popup = view.open(url, '_blank');
if(!popup) view.location.href = url;
url=undefined; // release reference before dispatching
filesaver.readyState = filesaver.DONE;
dispatch_all();
};
reader.readAsDataURL(blob);
filesaver.readyState = filesaver.INIT;
return;
}
// don't create more object URLs than needed
if (!object_url) {
object_url = get_URL().createObjectURL(blob);
}
if (force) {
view.location.href = object_url;
} else {
var opened = view.open(object_url, "_blank");
if (!opened) {
// Apple does not allow window.open, see https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/WorkingwithWindowsandTabs/WorkingwithWindowsandTabs.html
view.location.href = object_url;
}
}
filesaver.readyState = filesaver.DONE;
dispatch_all();
revoke(object_url);
}
;
filesaver.readyState = filesaver.INIT;
if (can_use_save_link) {
object_url = get_URL().createObjectURL(blob);
setImmediate(function() {
save_link.href = object_url;
save_link.download = name;
click(save_link);
dispatch_all();
revoke(object_url);
filesaver.readyState = filesaver.DONE;
}, 0);
return;
}
fs_error();
}
, FS_proto = FileSaver.prototype
, saveAs = function(blob, name, no_auto_bom) {
return new FileSaver(blob, name || blob.name || "download", no_auto_bom);
}
;
// IE 10+ (native saveAs)
if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) {
return function(blob, name, no_auto_bom) {
name = name || blob.name || "download";
if (!no_auto_bom) {
blob = auto_bom(blob);
}
return navigator.msSaveOrOpenBlob(blob, name);
};
}
// todo: detect chrome extensions & packaged apps
//save_link.target = "_blank";
FS_proto.abort = function(){};
FS_proto.readyState = FS_proto.INIT = 0;
FS_proto.WRITING = 1;
FS_proto.DONE = 2;
FS_proto.error =
FS_proto.onwritestart =
FS_proto.onprogress =
FS_proto.onwrite =
FS_proto.onabort =
FS_proto.onerror =
FS_proto.onwriteend =
null;
return saveAs;
}(
typeof self !== "undefined" && self
|| typeof window !== "undefined" && window
|| this
));