Unable to download excel file from src assets in development qa prod environment for angular application

0 votes

I'm using an application built with Angular 7. In src/assets, I have a sample excel file. To download an example excel file, I've built a download attachment technique. I can download it, but when I view the downloaded sample excel file, I receive an error. Excel cannot open the file "sample.xlsx" because the file format or file extension is invalid, according to the error message. Check to make sure the file is not corrupted and that the file extension corresponds to the file's format.

component.component.html

<button mat-raised-button (click)="downloadexcelfile()">
<button mat-raised-button (click)="download()">

component.component.ts

downloadexcelfile() {

if(navigator.msSaveBlob) {
// to support in IE 10+
let data: any;
this.HttpClient.get("./assets/sample.xlsx",{responseType: "blob"}).subscribe((res:any) => { 
data = res;

// approach - 1
let csvData = new Blob([data], { type: 'text/csv;charset=utf-8;'});

// approach - 2
let csvData = new Blob([data], { type: 'application/vnd.ms-excel'});

// approach - 3
let csvData = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});

navigator.msSaveBlob(csvData, "sample.xlsx");
})

}

else if(!navigator.msSaveBlob) {
let link = document.createElement("a");
link.href = "assets/sample.xlsx";
link.download = "sample.xlsx";
link.click();
window.URL.revokeObjectURL(link.href);
link.remove();
}

}

// approach - 4
download() {

if(navigator.msSaveBlob) {
// to support in IE 10+
let data: any;
this.HttpClient.get("./assets/sample.xlsx",{responseType: "arraybuffer"}).subscribe((res:any) => { 
data = res;

// approach - 5
let csvData = new Blob([data], { type: 'text/csv'});

// approach - 6
let csvData = new Blob([data], { type: 'application/vnd.ms-excel'});

// approach - 7
let csvData = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});

navigator.msSaveBlob(csvData, "sample.xlsx");
})

}

else if(!navigator.msSaveBlob) {
let link = document.createElement("a");
link.href = "assets/sample.xlsx";
link.download = "sample.xlsx";
link.click();
window.URL.revokeObjectURL(link.href);
link.remove();
}

}
Nov 12 in Others by Kithuzzz
• 20,660 points
82 views

1 answer to this question.

0 votes

Try this:

   this.HttpClient.get("./assets/sample.xlsx",{responseType: "blob"}).subscribe((res:any) => { 
          const url= window.URL.createObjectURL(res);
          window.open(url);
    });
answered Nov 12 by narikkadan
• 37,660 points

Related Questions In Others

0 votes
0 answers

How to handle large http response data from observer in Angular application to avoid browser crash?

Suppose we have a angular application which ...READ MORE

Apr 19, 2019 in Others by Hemant Gajbe
1,762 views
0 votes
2 answers
0 votes
1 answer
0 votes
1 answer

How to apply zoom animation for each element of a list in angular?

Hey @Sid, do check if this link ...READ MORE

answered Jul 30, 2019 in Others by Vardhan
• 13,200 points
618 views
0 votes
1 answer

What are the vulnerability related to PHP Form?

Hii, The $_SERVER["PHP_SELF"] variable can be used by ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,800 points
1,581 views
+1 vote
1 answer

How can we send message multiple time to a specific person or group in whatsapp using loop?

Hii @kartik,  This is simple task to send single ...READ MORE

answered Feb 28, 2020 in Java-Script by Niroj
• 82,800 points
8,157 views
0 votes
1 answer

Why it is necessary to refresh CSRF token per form request?

Hello, Generating a new CSRF token for each ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,800 points
3,128 views
0 votes
1 answer

What is meant by passing the variable by value and reference in PHP?

Hello, When the variable is passed as value ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,800 points
1,779 views
0 votes
1 answer

Unable to import data in excel from another website using VB code

Replace : Set ieTable = ieDoc.all.Item("report-table") With: Set ieTable = ...READ MORE

answered Sep 21 in Others by narikkadan
• 37,660 points
73 views
0 votes
1 answer

Download file from URL in Excel 2019 (it works on Excel 2007)

The Sub Code looks fine. Check the ...READ MORE

answered Oct 7 in Others by narikkadan
• 37,660 points
111 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP