From 1d00646b17565fef1563f6e28cf1a2d08b11adbb Mon Sep 17 00:00:00 2001 From: Danny Coates Date: Wed, 21 Feb 2018 15:43:32 -0800 Subject: [PATCH] go back to faster createObjectURL for saveFile on non-iOS --- app/fileReceiver.js | 46 ++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/app/fileReceiver.js b/app/fileReceiver.js index cd3e2f2c..b7462c0b 100644 --- a/app/fileReceiver.js +++ b/app/fileReceiver.js @@ -95,27 +95,39 @@ export default class FileReceiver extends Nanobus { async function saveFile(file) { return new Promise(function(resolve, reject) { - const reader = new FileReader(); const dataView = new DataView(file.plaintext); const blob = new Blob([dataView], { type: file.type }); - if (window.navigator.msSaveBlob) { - window.navigator.msSaveBlob(blob, file.name); + if (navigator.msSaveBlob) { + navigator.msSaveBlob(blob, file.name); return resolve(); + } else if (/iPhone|fxios/i.test(navigator.userAgent)) { + // This method is much slower but createObjectURL + // is buggy on iOS + const reader = new FileReader(); + reader.addEventListener('loadend', function() { + if (reader.error) { + return reject(reader.error); + } + if (reader.result) { + const a = document.createElement('a'); + a.href = reader.result; + a.download = file.name; + document.body.appendChild(a); + a.click(); + } + resolve(); + }); + reader.readAsDataURL(blob); + } else { + const downloadUrl = URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = downloadUrl; + a.download = file.name; + document.body.appendChild(a); + a.click(); + URL.revokeObjectURL(downloadUrl); + setTimeout(resolve, 100); } - reader.addEventListener('loadend', function() { - if (reader.error) { - return reject(reader.error); - } - if (reader.result) { - const a = document.createElement('a'); - a.href = reader.result; - a.download = file.name; - document.body.appendChild(a); - a.click(); - } - resolve(); - }); - reader.readAsDataURL(blob); }); }