mail/src/js/util/download.js

66 lines
2.1 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
2013-10-23 11:17:36 -04:00
var ngModule = angular.module('woUtil');
ngModule.service('download', Download);
module.exports = Download;
2014-10-02 16:05:44 -04:00
var util = require('crypto-lib').util;
/**
* A download helper to abstract platform specific behavior
*/
function Download() {}
2013-10-23 11:17:36 -04:00
/**
* Create download link and click on it.
*/
Download.prototype.createDownload = function(options) {
2014-10-02 16:05:44 -04:00
var contentType = options.contentType || 'application/octet-stream';
var filename = options.filename || 'file';
var content = options.content;
var a = document.createElement('a');
var supportsBlob;
2013-11-04 08:20:14 -05:00
2014-10-02 16:05:44 -04:00
try {
supportsBlob = !!new Blob();
} catch (e) {}
2013-11-04 08:20:14 -05:00
2014-10-02 16:05:44 -04:00
if (typeof a.download !== "undefined" && supportsBlob) {
// ff 30+, chrome 27+ (android: 37+)
document.body.appendChild(a);
a.style = "display: none";
a.href = window.URL.createObjectURL(new Blob([content], {
type: contentType
}));
a.download = filename;
a.click();
setTimeout(function() {
window.URL.revokeObjectURL(a.href);
document.body.removeChild(a);
}, 10); // arbitrary, just get it off the main thread
} else if (window.navigator.msSaveBlob) {
// ie 10+
window.navigator.msSaveBlob(new Blob([content], {
type: contentType
}), filename);
} else if (supportsBlob) {
// safari actually makes no sense:
// - you can't open a new window
// - the file system api is dead
// - download attribute doesn't work
// - behaves randomly (opens a new tab or doesn't, downloads stuff or doesn't, ...)
var url = window.URL.createObjectURL(new Blob([content], {
type: contentType
}));
var newTab = window.open(url, "_blank");
if (!newTab) {
window.location.href = url;
2013-11-04 08:20:14 -05:00
}
2014-10-02 16:05:44 -04:00
} else {
// anything else, where anything at all is better than nothing
if (typeof content !== "string" && content.buffer) {
content = util.arrBuf2BinStr(content.buffer);
}
window.open('data:' + contentType + ';base64,' + btoa(content), "_blank");
}
};