fetch download file code example

Example: fetch download blob file

try {
            fetch(URL + 'download_estudiante_excel', {
                method: 'GET',
                headers: {
                    'Content-Type': '\tapplication/vnd.ms-excel',
                    'Authorization': 'Bearer ' + sessionStorage.getItem('token')
                },
            }).then(
                data => {
                    return data.blob();
                }
            ).then(
                response => {
                    console.log(response.type);
                    const dataType = response.type;
                    const binaryData = [];
                    binaryData.push(response);
                    const downloadLink = document.createElement('a');
                    downloadLink.href = window.URL.createObjectURL(new Blob(binaryData, { type: dataType }));
                    downloadLink.setAttribute('download', 'report');
                    document.body.appendChild(downloadLink);
                    downloadLink.click();
                    downloadLink.remove();
                }
            )

        } catch (e) {
            addToast('Error inesperado.', {appearance: 'error', autoDismiss: true});
        }