Created
October 23, 2019 17:54
-
-
Save TiagoFuelber/e63ce7b2c8d72f213c21cbd173d5dedb to your computer and use it in GitHub Desktop.
Download file function that works on IE
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
import { IS_IE } from '../view/ui/styles/constants'; | |
const getByteArray = arquivo => { | |
const byteCharacters = atob(arquivo); | |
const byteNumbers = new Array(byteCharacters.length); | |
for (let i = 0; i < byteCharacters.length; i++) { | |
byteNumbers[i] = byteCharacters.charCodeAt(i); | |
} | |
return new Uint8Array(byteNumbers); | |
}; | |
const download = async (arquivo, nomeArquivo) => { | |
const byteArray = getByteArray(arquivo); | |
const blob = new Blob([byteArray], { | |
type: 'application/pdf; application/json; charset=utf-8' | |
}); | |
if (IS_IE) { | |
window.navigator.msSaveOrOpenBlob(blob, nomeArquivo); | |
} else { | |
const a = document.createElement('a'); | |
a.href = URL.createObjectURL(blob); | |
a.download = nomeArquivo; | |
document.body.appendChild(a); | |
a.click(); | |
document.body.removeChild(a); | |
} | |
}; | |
export default download; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment