go back to faster createObjectURL for saveFile on non-iOS

This commit is contained in:
Danny Coates 2018-02-21 15:43:32 -08:00
parent e4b98fe65a
commit 1d00646b17
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
1 changed files with 29 additions and 17 deletions

View File

@ -95,14 +95,16 @@ export default class FileReceiver extends Nanobus {
async function saveFile(file) { async function saveFile(file) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
const reader = new FileReader();
const dataView = new DataView(file.plaintext); const dataView = new DataView(file.plaintext);
const blob = new Blob([dataView], { type: file.type }); const blob = new Blob([dataView], { type: file.type });
if (window.navigator.msSaveBlob) { if (navigator.msSaveBlob) {
window.navigator.msSaveBlob(blob, file.name); navigator.msSaveBlob(blob, file.name);
return resolve(); 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() { reader.addEventListener('loadend', function() {
if (reader.error) { if (reader.error) {
return reject(reader.error); return reject(reader.error);
@ -117,5 +119,15 @@ async function saveFile(file) {
resolve(); resolve();
}); });
reader.readAsDataURL(blob); 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);
}
}); });
} }