0

I have file stored in database table with the contents(bytes), fileName, contentType. I want the file to open in new tab when clicked in the Angular side with the fileName that is retrieved from database. My file opens in the new tab but how can I give the fileName to it.

//Service
downloadDoc(Id: string): Observable<any> {
let url = this.APIUrl + Id + '/doc';
let headers: HttpHeaders = new HttpHeaders();
return this.http.get(url, { headers: headers, responseType: 'blob' });   
}

//Component
getDoc(id: any) {
this.service.downloadDoc(id).subscribe((result) => {
  var url = window.URL.createObjectURL(result); 
  window.open(url, '_blank');     
});
}

// Web api
[HttpGet("{id}/doc")]       
public FileResult GetDoc(int id)
{
   HDoc hdoc = _hDocManager.GetDoc(id);            
   return File(hdoc.Contents, hdoc.ContentType, hdoc.FileName);            
}
PAR
  • 707
  • 1
  • 6
  • 18
  • What is the file name you are getting right now? Ideally this is done by the API which you are calling to get the file. – Chetan Jun 05 '20 at 23:44
  • I get characters like bd61ceaf-2675-4298-a787-0f46ffc5f4b2. blob:http://localhost:4200/bd61ceaf-2675-4298-a787-0f46ffc5f4b2 – PAR Jun 05 '20 at 23:50
  • What is the value of `hdoc.FileName`? `_hDocManager.GetDoc(id)` is your code? – Chetan Jun 06 '20 at 00:06
  • hdoc.FileName has the name of the file. hDoc corresponds to the table which has fileName, contents and contentType – PAR Jun 06 '20 at 00:31
  • https://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-asp-net-mvc – Chetan Jun 06 '20 at 00:54

0 Answers0