1
0
mirror of https://gitlab.com/timvisee/send.git synced 2024-11-10 13:13:00 +01:00

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

View File

@ -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);
});
}