diff --git a/.babelrc b/.babelrc deleted file mode 100644 index 14c383d..0000000 --- a/.babelrc +++ /dev/null @@ -1,19 +0,0 @@ -{ - "presets": [ - ["env", { - "targets": { - "browsers": [ - "ie > 9" - ] - } - }] - ], - "plugins": [ - "transform-es2015-modules-umd" - ], - "env": { - "production": { - "presets": ["minify"] - } - } -} diff --git a/.gitignore b/.gitignore index 7dd21db..eaf3961 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,3 @@ node_modules/ .DS_Store .idea/ - -# Generated files -/dist/ diff --git a/README.md b/README.md index 216332c..3a61a84 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -If you need to save really large files bigger then the blob's size limitation or don't have +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 @@ -58,16 +58,16 @@ saveAs must be run within a user interaction event such as onTouchDown or onClic Syntax ------ -### Import saveAs() from file-saver +### Import saveAs() from file-saver ```js import { saveAs } from 'file-saver/FileSaver'; ``` ```js -FileSaver saveAs(Blob/File data, optional DOMString filename, optional Boolean disableAutoBOM) +FileSaver saveAs(Blob/File/Url, optional DOMString filename, optional Boolean autoBOM) ``` -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 `autoBOM` 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)). Examples -------- @@ -86,6 +86,16 @@ var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"}); FileSaver.saveAs(blob, "hello world.txt"); ``` +### Saving urls + +```js +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 synchronously head request +if it dose it will download the data and save it using blob urls +if not it will try to download it using `a[download]` + The standard W3C File API [`Blob`][4] interface is not available in all browsers. [Blob.js][5] is a cross-browser `Blob` implementation that solves this. diff --git a/bower.json b/bower.json index 32dd2ad..b62bd7e 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "file-saver", - "main": "dist/FileSaver.js", - "version": "1.3.8", + "main": "dist/FileSaver.min.js", + "version": "2.0.0-rc.1", "homepage": "https://github.com/eligrey/FileSaver.js", "authors": [ "Eli Grey " @@ -16,6 +16,7 @@ "ignore": [ "*", "!FileSaver.*js", + "!FileSaver.*map", "!LICENSE.md" ] } diff --git a/demo/demo.css b/demo/demo.css deleted file mode 100644 index de4a78d..0000000 --- a/demo/demo.css +++ /dev/null @@ -1,50 +0,0 @@ -html { - background-color: #DDD; -} -body { - -webkit-box-sizing: content-box; - -moz-box-sizing: content-box; - box-sizing: content-box; - width: 900px; - margin: 0 auto; - font-family: Verdana, Helvetica, Arial, sans-serif; - -webkit-box-shadow: 0 0 10px 2px rgba(0, 0, 0, .5); - -moz-box-shadow: 0 0 10px 2px rgba(0, 0, 0, .5); - box-shadow: 0 0 10px 2px rgba(0, 0, 0, .5); - padding: 7px 25px 70px; - background-color: #FFF; -} -h1, h2, h3, h4, h5, h6 { - font-family: Georgia, "Times New Roman", serif; -} -h2, form { - text-align: center; -} -form { - margin-top: 5px; -} -.input { - width: 500px; - height: 300px; - margin: 0 auto; - display: block; -} -section { - margin-top: 40px; -} -#canvas { - cursor: crosshair; -} -#canvas, #html { - border: 1px solid #000; -} -.filename { - text-align: right; -} -#html { - -webkit-box-sizing: border-box; - -moz-box-sizing: border-box; - box-sizing: border-box; - overflow: auto; - padding: 1em; -} diff --git a/demo/demo.js b/demo/demo.js deleted file mode 100755 index 2b3dc34..0000000 --- a/demo/demo.js +++ /dev/null @@ -1,216 +0,0 @@ -/*! FileSaver.js demo script - * 2016-05-26 - * - * By Eli Grey, http://eligrey.com - * License: MIT - * See LICENSE.md - */ - -/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/demo/demo.js */ - -/*jshint laxbreak: true, laxcomma: true, smarttabs: true*/ -/*global saveAs, self*/ - -(function(view) { -"use strict"; -// The canvas drawing portion of the demo is based off the demo at -// http://www.williammalone.com/articles/create-html5-canvas-javascript-drawing-app/ -var - document = view.document - , $ = function(id) { - return document.getElementById(id); - } - , session = view.sessionStorage - // only get URL when necessary in case Blob.js hasn't defined it yet - , get_blob = function() { - return view.Blob; - } - - , canvas = $("canvas") - , canvas_options_form = $("canvas-options") - , canvas_filename = $("canvas-filename") - , canvas_clear_button = $("canvas-clear") - - , text = $("text") - , text_options_form = $("text-options") - , text_filename = $("text-filename") - - , html = $("html") - , html_options_form = $("html-options") - , html_filename = $("html-filename") - - , ctx = canvas.getContext("2d") - , drawing = false - , x_points = session.x_points || [] - , y_points = session.y_points || [] - , drag_points = session.drag_points || [] - , add_point = function(x, y, dragging) { - x_points.push(x); - y_points.push(y); - drag_points.push(dragging); - } - , draw = function(){ - canvas.width = canvas.width; - ctx.lineWidth = 6; - ctx.lineJoin = "round"; - ctx.strokeStyle = "#000000"; - var - i = 0 - , len = x_points.length - ; - for(; i < len; i++) { - ctx.beginPath(); - if (i && drag_points[i]) { - ctx.moveTo(x_points[i-1], y_points[i-1]); - } else { - ctx.moveTo(x_points[i]-1, y_points[i]); - } - ctx.lineTo(x_points[i], y_points[i]); - ctx.closePath(); - ctx.stroke(); - } - } - , stop_drawing = function() { - drawing = false; - } - - // Title guesser and document creator available at https://gist.github.com/1059648 - , guess_title = function(doc) { - var - h = "h6 h5 h4 h3 h2 h1".split(" ") - , i = h.length - , headers - , header_text - ; - while (i--) { - headers = doc.getElementsByTagName(h[i]); - for (var j = 0, len = headers.length; j < len; j++) { - header_text = headers[j].textContent.trim(); - if (header_text) { - return header_text; - } - } - } - } - , doc_impl = document.implementation - , create_html_doc = function(html) { - var - dt = doc_impl.createDocumentType('html', null, null) - , doc = doc_impl.createDocument("http://www.w3.org/1999/xhtml", "html", dt) - , doc_el = doc.documentElement - , head = doc_el.appendChild(doc.createElement("head")) - , charset_meta = head.appendChild(doc.createElement("meta")) - , title = head.appendChild(doc.createElement("title")) - , body = doc_el.appendChild(doc.createElement("body")) - , i = 0 - , len = html.childNodes.length - ; - charset_meta.setAttribute("charset", html.ownerDocument.characterSet); - for (; i < len; i++) { - body.appendChild(doc.importNode(html.childNodes.item(i), true)); - } - var title_text = guess_title(doc); - if (title_text) { - title.appendChild(doc.createTextNode(title_text)); - } - return doc; - } -; -canvas.width = 500; -canvas.height = 300; - - if (typeof x_points === "string") { - x_points = JSON.parse(x_points); -} if (typeof y_points === "string") { - y_points = JSON.parse(y_points); -} if (typeof drag_points === "string") { - drag_points = JSON.parse(drag_points); -} if (session.canvas_filename) { - canvas_filename.value = session.canvas_filename; -} if (session.text) { - text.value = session.text; -} if (session.text_filename) { - text_filename.value = session.text_filename; -} if (session.html) { - html.innerHTML = session.html; -} if (session.html_filename) { - html_filename.value = session.html_filename; -} - -drawing = true; -draw(); -drawing = false; - -canvas_clear_button.addEventListener("click", function() { - canvas.width = canvas.width; - x_points.length = - y_points.length = - drag_points.length = - 0; -}, false); -canvas.addEventListener("mousedown", function(event) { - event.preventDefault(); - drawing = true; - add_point(event.pageX - canvas.offsetLeft, event.pageY - canvas.offsetTop, false); - draw(); -}, false); -canvas.addEventListener("mousemove", function(event) { - if (drawing) { - add_point(event.pageX - canvas.offsetLeft, event.pageY - canvas.offsetTop, true); - draw(); - } -}, false); -canvas.addEventListener("mouseup", stop_drawing, false); -canvas.addEventListener("mouseout", stop_drawing, false); - -canvas_options_form.addEventListener("submit", function(event) { - event.preventDefault(); - canvas.toBlobHD(function(blob) { - saveAs( - blob - , (canvas_filename.value || canvas_filename.placeholder) + ".png" - ); - }, "image/png"); -}, false); - -text_options_form.addEventListener("submit", function(event) { - event.preventDefault(); - var BB = get_blob(); - saveAs( - new BB( - [text.value || text.placeholder] - , {type: "text/plain;charset=" + document.characterSet} - ) - , (text_filename.value || text_filename.placeholder) + ".txt" - ); -}, false); - -html_options_form.addEventListener("submit", function(event) { - event.preventDefault(); - var - BB = get_blob() - , xml_serializer = new XMLSerializer() - , doc = create_html_doc(html) - ; - saveAs( - new BB( - [xml_serializer.serializeToString(doc)] - , {type: "text/plain;charset=" + document.characterSet} - ) - , (html_filename.value || html_filename.placeholder) + ".xhtml" - ); -}, false); - -view.addEventListener("unload", function() { - session.x_points = JSON.stringify(x_points); - session.y_points = JSON.stringify(y_points); - session.drag_points = JSON.stringify(drag_points); - session.canvas_filename = canvas_filename.value; - - session.text = text.value; - session.text_filename = text_filename.value; - - session.html = html.innerHTML; - session.html_filename = html_filename.value; -}, false); -}(self)); diff --git a/demo/demo.min.js b/demo/demo.min.js deleted file mode 100755 index d30333c..0000000 --- a/demo/demo.min.js +++ /dev/null @@ -1,2 +0,0 @@ -/*! @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 - - - - FileSaver.js demo - - - -

FileSaver.js demo

-

- The following examples demonstrate how it is possible to generate and save any type of data right in the browser using the saveAs() FileSaver interface, without contacting any servers. -

-
-

Saving an image

- -
- - - -
-
-
-

Saving text

-