Skip to content

Instantly share code, notes, and snippets.

@TiagoFuelber
Created October 23, 2019 17:54
Show Gist options
  • Save TiagoFuelber/e63ce7b2c8d72f213c21cbd173d5dedb to your computer and use it in GitHub Desktop.
Save TiagoFuelber/e63ce7b2c8d72f213c21cbd173d5dedb to your computer and use it in GitHub Desktop.
Download file function that works on IE
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