1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00

Avoid blob url for files downloads (#17986)

This commit is contained in:
weizman 2023-03-07 12:47:45 +02:00 committed by GitHub
parent f46ce9f2bd
commit bfbd65237e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,19 +1,13 @@
import { getRandomFileName } from './util'; import { getRandomFileName } from './util';
export function exportAsFile(filename, data, type = 'text/csv') { export function exportAsFile(filename, data, type = 'text/csv') {
const b64 = Buffer.from(data, 'utf8').toString('base64');
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
filename = filename || getRandomFileName(); filename = filename || getRandomFileName();
// source: https://stackoverflow.com/a/33542499 by Ludovic Feltz
const blob = new window.Blob([data], { type });
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, filename);
} else {
const elem = window.document.createElement('a'); const elem = window.document.createElement('a');
elem.target = '_blank'; elem.href = `data:${type};Base64,${b64}`;
elem.href = window.URL.createObjectURL(blob);
elem.download = filename; elem.download = filename;
document.body.appendChild(elem); document.body.appendChild(elem);
elem.click(); elem.click();
document.body.removeChild(elem); document.body.removeChild(elem);
}
} }