ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 278k

How to download Excel template file from folder attachment using angul

Nov 22 2020 6:31 PM
I work on project angular 7 and web API asp.net core 2.2 I face issue I can't get Template Excel file
 
Exist on Server found on folder Attachment and file Name Delivery.xlsx to download it .
 
I only Need When click on download Button download Template Excel file from folder attachment
 
my issue How to get file Delivery.xlsx from attachment Folder and assign it on angular 7 to download ?
 
component.html
  1. <input type="button" class="btn btn-success" value="Download" (click)="download()" style="margin: 20px 0;"/>   
component.ts
  1. download(file) {    
  2.           
  3.     let fileName = ???;    
  4.     let checkFileType = fileName.split('.').pop();    
  5.     var fileType;    
  6.        
  7.     if (checkFileType == ".xlsx") {    
  8.         fileType = "application/vnd.openxmlformats officedocument.spreadsheetml.sheet";    
  9.     }    
  10.         
  11.     this.DownloadFile(fileName, fileType)    
  12.         .subscribe(    
  13.             success => {    
  14.                 saveAs(success, fileName);    
  15.             },    
  16.             err => {    
  17.                 alert("Server error while downloading file.");    
  18.             }    
  19.         );    
  20. }    
  21.     
  22. DownloadFile(filePath: string, fileType: string): Observable<any> {    
  23.     
  24.     let fileExtension = fileType;    
  25.     let input = filePath;    
  26.     
  27.     return this.http.get('https://localhost:44396/api/ApprovalQuality/download'"?fileName=" + input, {    
  28.            
  29.         responseType: 'blob',    
  30.         observe: 'response'    
  31.     })    
  32.         .pipe(    
  33.             map((res: any) => {    
  34.                 return new Blob([res.body], { type: fileExtension });    
  35.             })    
  36.         );    
  37. }  
Web API .net core 2.2
  1. [HttpGet]    
  2. [Route("Download")]    
  3. public IActionResult Download()    
  4. {    
  5.     string filename = null;    
  6.     if (filename == null)    
  7.         return Content("filename not present");    
  8.   
  9.     var path = Path.Combine(    
  10.                    Directory.GetCurrentDirectory(),    
  11.                    "Attachment", filename);    
  12.   
  13.     var memory = new MemoryStream();    
  14.     using (var stream = new FileStream(path, FileMode.Open))    
  15.     {    
  16.          stream.CopyTo(memory);    
  17.     }    
  18.     memory.Position = 0;    
  19.     ExcelEx ex = new ExcelEx();    
  20.     return File(memory, ex.GetContentType(path), Path.GetFileName(path));    
  21. }

Answers (1)