fetch download blob file

Solutions on MaxInterview for fetch download blob file by the best coders in the world

showing results for - "fetch download blob file"
Isabel
26 Sep 2020
1try {
2            fetch(URL + 'download_estudiante_excel', {
3                method: 'GET',
4                headers: {
5                    'Content-Type': '\tapplication/vnd.ms-excel',
6                    'Authorization': 'Bearer ' + sessionStorage.getItem('token')
7                },
8            }).then(
9                data => {
10                    return data.blob();
11                }
12            ).then(
13                response => {
14                    console.log(response.type);
15                    const dataType = response.type;
16                    const binaryData = [];
17                    binaryData.push(response);
18                    const downloadLink = document.createElement('a');
19                    downloadLink.href = window.URL.createObjectURL(new Blob(binaryData, { type: dataType }));
20                    downloadLink.setAttribute('download', 'report');
21                    document.body.appendChild(downloadLink);
22                    downloadLink.click();
23                    downloadLink.remove();
24                }
25            )
26
27        } catch (e) {
28            addToast('Error inesperado.', {appearance: 'error', autoDismiss: true});
29        }