go back to faster createObjectURL for saveFile on non-iOS
This commit is contained in:
parent
e4b98fe65a
commit
1d00646b17
@ -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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user