-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid blob url for files downloads (#17986)
- Loading branch information
Showing
1 changed file
with
7 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,13 @@ | ||
import { getRandomFileName } from './util'; | ||
|
||
export function exportAsFile(filename, data, type = 'text/csv') { | ||
const b64 = Buffer.from(data, 'utf8').toString('base64'); | ||
// eslint-disable-next-line no-param-reassign | ||
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'); | ||
elem.target = '_blank'; | ||
elem.href = window.URL.createObjectURL(blob); | ||
elem.download = filename; | ||
document.body.appendChild(elem); | ||
elem.click(); | ||
document.body.removeChild(elem); | ||
} | ||
const elem = window.document.createElement('a'); | ||
elem.href = `data:${type};Base64,${b64}`; | ||
elem.download = filename; | ||
document.body.appendChild(elem); | ||
elem.click(); | ||
document.body.removeChild(elem); | ||
} |