From d3b8ef44ed9236fa4e5958ef3f93bb6078a0af0a Mon Sep 17 00:00:00 2001 From: Alexander Do Date: Sat, 7 Apr 2018 06:29:36 +0000 Subject: [PATCH] Download Attachment changes. Support for Edge and change to Blob --- js/privatebin.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/js/privatebin.js b/js/privatebin.js index a39c38a..dffa4a2 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -1925,7 +1925,34 @@ jQuery.PrivateBin = (function($, sjcl, Base64, RawDeflate) { { var imagePrefix = 'data:image/'; - $attachmentLink.attr('href', attachmentData); + if (window.Blob) { + var base64Start = attachmentData.indexOf(',') + 1; + var contentTypeEnd = attachmentData.indexOf(';'); + + var contentType = attachmentData.substring(5, contentTypeEnd); + var buf = Base64.atob(attachmentData.substring(base64Start)); + 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 }); + + if (navigator.msSaveBlob) { + $attachmentLink.bind('click', function () { + navigator.msSaveBlob(file, fileName); + }); + } else if (window.URL && window.URL.createObjectURL) { + $attachmentLink.attr('href', window.URL.createObjectURL(file)); + } else { + $attachmentLink.attr('href', attachmentData); + } + } else { + $attachmentLink.attr('href', attachmentData); + } + if (typeof fileName !== 'undefined') { $attachmentLink.attr('download', fileName); } @@ -1971,6 +1998,7 @@ jQuery.PrivateBin = (function($, sjcl, Base64, RawDeflate) { me.hideAttachmentPreview(); $attachmentLink.prop('href', ''); $attachmentLink.prop('download', ''); + $attachmentLink.unbind('click'); $attachmentPreview.html(''); };