angular-download
Service for downloading browser-generated data to disk.
Motivation
This service provides methods for downloading content generated in JavaScript (text, images, etc.) as files.
It relies on the <a download>
functionality supported in current browsers and does not require any server
handling or flash plugins.
Installing the Module
Installation can be done through bower:
bower install angular-download
In your page add:
You can also use npm to install it:
npm install angular-download --save
Loading the Module
This service is part of the download
module:
var app = angular;
Using the download service
Inject the download
service:
{ $scope { download; }}
Method: fromData(data, mimeType, fileName)
Downloads a file containing string data
as type mimeType
named fileName
.
{ $scope { download; }}
Method: fromBase64(dataBase64, mimeType, fileName)
Downloads a file containing Base64-encoded string dataBase64
as type mimeType
named fileName
.
{ $scope { download; }}
Method: fromDataURL(dataUrl, fileName)
Downloads a file with contents defined by the dataUrl
named fileName
. This is useful for downloading binary
data, such as client-generated images.
{ $scope { download; }} // Create a dataURL from an img element { // Create an empty canvas element var canvas = document; // Copy the image contents to the canvas canvaswidth = imgwidth; canvasheight = imgheight; var ctx = canvas; ctx; return canvas;}
Method: fromBlob(dataBlob, fileName)
Downloads a Blob
with contents defined by the dataBlob
named fileName
. This is useful for downloading binary
data, such as client-generated images.
{ $scope { ; }} // Create a PNG Blob from an img element { // Create an empty canvas element var canvas = document; // Copy the image contents to the canvas canvaswidth = imgwidth; canvasheight = imgheight; var ctx = canvas; ctx; if canvasmsToBlob // On IE, the msToBlob method returns the Blob value ; else // On standard browsers, the toBlob method calls the callback canvas; }
Copyright & License
Copyright 2015 Stepan Riha. All Rights Reserved.
This may be redistributed under the MIT licence. For the full license terms, see the LICENSE file which should be alongside this readme.