Only create Blob for Download for IE upon click event

This commit is contained in:
Alexander Do 2018-04-09 04:44:37 +00:00
parent 2925fa8bfc
commit 60cedd7fb5

View File

@ -1928,30 +1928,29 @@ jQuery.PrivateBin = (function($, sjcl, Base64, RawDeflate) {
// IE does not support setting a data URI on an a element // IE does not support setting a data URI on an a element
// Convert dataURI to a Blob and use msSaveBlob to download // Convert dataURI to a Blob and use msSaveBlob to download
if (window.Blob && navigator.msSaveBlob) { if (window.Blob && navigator.msSaveBlob) {
// data URI format: data:[<contentType>][;base64],<data> $attachmentLink.bind('click', function () {
// data URI format: data:[<mediaType>][;base64],<data>
// position in data URI string of where data begins // position in data URI string of where data begins
var base64Start = attachmentData.indexOf(',') + 1; var base64Start = attachmentData.indexOf(',') + 1;
// position in data URI string of where contentType ends // position in data URI string of where mediaType ends
var contentTypeEnd = attachmentData.indexOf(';'); var mediaTypeEnd = attachmentData.indexOf(';');
// extract contentType // extract mediaType
var contentType = attachmentData.substring(5, contentTypeEnd); var mediaType = attachmentData.substring(5, mediaTypeEnd);
// extract data and convert to binary // extract data and convert to binary
var buf = Base64.atob(attachmentData.substring(base64Start)); var decodedData = Base64.atob(attachmentData.substring(base64Start));
// Transform into a Blob // Transform into a Blob
var bufLength = buf.length; var decodedDataLength = decodedData.length;
var array = new Uint8Array(bufLength); var buf = new Uint8Array(decodedDataLength);
for (var i = 0; i < bufLength; i++) { for (var i = 0; i < decodedDataLength; i++) {
array[i] = buf.charCodeAt(i); buf[i] = decodedData.charCodeAt(i);
} }
var file = new window.Blob([ array ], { type: contentType }); var blob = new window.Blob([ buf ], { type: mediaType });
navigator.msSaveBlob(blob, fileName);
$attachmentLink.bind('click', function () {
navigator.msSaveBlob(file, fileName);
}); });
} else { } else {
$attachmentLink.attr('href', attachmentData); $attachmentLink.attr('href', attachmentData);