file download is damaged when use observe: response in angular service

Mahdi

when I use observe: 'response' in angular service. To get the file name from the header and download it. The file is corrupted.

 return this.http.get(url, {observe: 'response', responseType: 'blob'});

    downloadLicenseFile() {
        let fileName;
        this.loading = true;
        this.licenseService.downLoadFile().subscribe(
            (success) => {
                fileName = success.headers.get('content-disposition').split('filename=')[1].replace(/['"]+/g, '');
                this.loading = false;
                this.validateNext = false;
                this.saveData(success, fileName);
            }, () => {
                this.validateNext = true;
                this.loading = false;
            });
    }


    saveData(data, fileName) {
        const a = document.createElement('a');
        document.body.appendChild(a);
        a.style.display = 'none';
        const blob = new Blob([data], { type: 'application/gzip' });
        const url = window.URL.createObjectURL(blob);
        a.href = url;
        a.download = fileName;
        a.click();
        window.URL.revokeObjectURL(url);
    }

If I delete observe: 'response', the downloaded file is correct.

Maryam Rajabi

This is very easy. You should use success.body instead success.

this.saveData(success.body, fileName);

Because when you use observe: 'response', the type of data received changes

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP, fpdf, damaged file download

From Dev

Angular Http Client File Download - File name from the response

From Dev

Download file served as response

From Dev

Download file served as response

From Dev

could not update a variable in an angular service with ajax response data and use is to filter

From Dev

could not update a variable in an angular service with ajax response data and use is to filter

From Dev

How to get response of a service in an array for use, in angular 2

From Dev

file "is damaged and can't be opened" when opening twice a mounted file

From Dev

Unauthorized response when downloading file with Service Account in Google Drive API

From Dev

How to observe objects in response JSON with no specyfic names in RXJS Angular 5

From Dev

Flask proxy response for file download

From Dev

Node.js - download and save PDF file locally for offline - file damaged

From Dev

How to Download a pdf Using the response from service

From Dev

Primefaces file download only works when I use the default constructor

From Dev

Download file via inbuilt download Service

From Dev

File download angular and Laravel

From Dev

Angular download SQLite file

From Dev

FFmpeg normalize damaged the file

From Dev

Dropnet web service download file

From Dev

Angular Service - Return http response

From Dev

Angular http$ response for text/plain file download fails with SyntaxError: Unexpected number in JSON

From Dev

Angular http$ response for text/plain file download fails with SyntaxError: Unexpected number in JSON

From Java

"The Response callback must not be null" when trying to send a PDF file download in Laravel 7

From Dev

Reenable a form submit button on response of a file download

From Dev

Download pdf file from ajax response

From Dev

How to download fetch response in react as file

From Dev

Django Pandas to http response (download file)

From Dev

Download a file from http response body

From Dev

Response::download make data in txt file loss

Related Related

  1. 1

    PHP, fpdf, damaged file download

  2. 2

    Angular Http Client File Download - File name from the response

  3. 3

    Download file served as response

  4. 4

    Download file served as response

  5. 5

    could not update a variable in an angular service with ajax response data and use is to filter

  6. 6

    could not update a variable in an angular service with ajax response data and use is to filter

  7. 7

    How to get response of a service in an array for use, in angular 2

  8. 8

    file "is damaged and can't be opened" when opening twice a mounted file

  9. 9

    Unauthorized response when downloading file with Service Account in Google Drive API

  10. 10

    How to observe objects in response JSON with no specyfic names in RXJS Angular 5

  11. 11

    Flask proxy response for file download

  12. 12

    Node.js - download and save PDF file locally for offline - file damaged

  13. 13

    How to Download a pdf Using the response from service

  14. 14

    Primefaces file download only works when I use the default constructor

  15. 15

    Download file via inbuilt download Service

  16. 16

    File download angular and Laravel

  17. 17

    Angular download SQLite file

  18. 18

    FFmpeg normalize damaged the file

  19. 19

    Dropnet web service download file

  20. 20

    Angular Service - Return http response

  21. 21

    Angular http$ response for text/plain file download fails with SyntaxError: Unexpected number in JSON

  22. 22

    Angular http$ response for text/plain file download fails with SyntaxError: Unexpected number in JSON

  23. 23

    "The Response callback must not be null" when trying to send a PDF file download in Laravel 7

  24. 24

    Reenable a form submit button on response of a file download

  25. 25

    Download pdf file from ajax response

  26. 26

    How to download fetch response in react as file

  27. 27

    Django Pandas to http response (download file)

  28. 28

    Download a file from http response body

  29. 29

    Response::download make data in txt file loss

HotTag

Archive