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, 2022 in Others by Kithuzzz
• 38,010 points
3,234 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);
    });

To know more about Angular, It's recommended to join Angular Certification Training Course today.

answered Nov 12, 2022 by narikkadan
• 63,700 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
2,476 views
0 votes
2 answers
0 votes
1 answer
0 votes
1 answer

How to unmerge multiple cells and transpose each value into a new column in Pandas dataframe from excel file

Try this: df = pd.read_excel("Sample_File.xlsx", header=[0,1,2,3,4,5], index_col = ...READ MORE

answered Jan 8, 2023 in Others by narikkadan
• 63,700 points
2,073 views
0 votes
1 answer

Reading Excel file using node.js

Several distinct libraries perform Excel file parsing ...READ MORE

answered Dec 10, 2022 in Others by narikkadan
• 63,700 points
1,732 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,880 points
2,837 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,880 points
17,834 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,880 points
4,228 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, 2022 in Others by narikkadan
• 63,700 points
573 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, 2022 in Others by narikkadan
• 63,700 points
1,529 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