Преглед изворни кода

Only create Blob for Download for IE upon click event

Alexander Do пре 8 година
родитељ
комит
60cedd7fb5
1 измењених фајлова са 18 додато и 19 уклоњено
  1. 18 19
      js/privatebin.js

+ 18 - 19
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:[<contentType>][;base64],<data>
-
-                // 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(';');
+                $attachmentLink.bind('click', function () {
+                    // data URI format: data:[<mediaType>][;base64],<data>
 
-                // extract contentType
-                var contentType = attachmentData.substring(5, contentTypeEnd);
-                // extract data and convert to binary
-                var buf = Base64.atob(attachmentData.substring(base64Start));
+                    // 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(';');
 
-                // Transform into a Blob
-                var bufLength = buf.length;
-                var array = new Uint8Array(bufLength);
+                    // extract mediaType
+                    var mediaType = attachmentData.substring(5, mediaTypeEnd);
+                    // extract data and convert to binary
+                    var decodedData = Base64.atob(attachmentData.substring(base64Start));
 
-                for (var i = 0; i < bufLength; i++) {
-                    array[i] = buf.charCodeAt(i);
-                }
+                    // Transform into a Blob
+                    var decodedDataLength = decodedData.length;
+                    var buf = new Uint8Array(decodedDataLength);
 
-                var file = new window.Blob([ array ], { type: contentType });
+                    for (var i = 0; i < decodedDataLength; i++) {
+                        buf[i] = decodedData.charCodeAt(i);
+                    }
 
-                $attachmentLink.bind('click', function () {
-                    navigator.msSaveBlob(file, fileName);
+                    var blob = new window.Blob([ buf ], { type: mediaType });
+                    navigator.msSaveBlob(blob, fileName);
                 });
             } else {
                 $attachmentLink.attr('href', attachmentData);