From 60cedd7fb56fb8add77fcd3e3a3126bef3598ade Mon Sep 17 00:00:00 2001 From: Alexander Do Date: Mon, 9 Apr 2018 04:44:37 +0000 Subject: [PATCH] Only create Blob for Download for IE upon click event --- js/privatebin.js | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/js/privatebin.js b/js/privatebin.js index f9fe5f9..9b1f33f 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -1928,30 +1928,29 @@ jQuery.PrivateBin = (function($, sjcl, Base64, RawDeflate) { // IE does not support setting a data URI on an a element // Convert dataURI to a Blob and use msSaveBlob to download if (window.Blob && navigator.msSaveBlob) { - // data URI format: data:[][;base64], - - // position in data URI string of where data begins - var base64Start = attachmentData.indexOf(',') + 1; - // position in data URI string of where contentType ends - var contentTypeEnd = attachmentData.indexOf(';'); - - // extract contentType - var contentType = attachmentData.substring(5, contentTypeEnd); - // extract data and convert to binary - var buf = Base64.atob(attachmentData.substring(base64Start)); - - // Transform into a Blob - var bufLength = buf.length; - var array = new Uint8Array(bufLength); - - for (var i = 0; i < bufLength; i++) { - array[i] = buf.charCodeAt(i); - } - - var file = new window.Blob([ array ], { type: contentType }); - $attachmentLink.bind('click', function () { - navigator.msSaveBlob(file, fileName); + // data URI format: data:[][;base64], + + // position in data URI string of where data begins + var base64Start = attachmentData.indexOf(',') + 1; + // position in data URI string of where mediaType ends + var mediaTypeEnd = attachmentData.indexOf(';'); + + // extract mediaType + var mediaType = attachmentData.substring(5, mediaTypeEnd); + // extract data and convert to binary + var decodedData = Base64.atob(attachmentData.substring(base64Start)); + + // Transform into a Blob + var decodedDataLength = decodedData.length; + var buf = new Uint8Array(decodedDataLength); + + for (var i = 0; i < decodedDataLength; i++) { + buf[i] = decodedData.charCodeAt(i); + } + + var blob = new window.Blob([ buf ], { type: mediaType }); + navigator.msSaveBlob(blob, fileName); }); } else { $attachmentLink.attr('href', attachmentData);