首页 > 解决方案 > Angular 9 Opening file saved in database with the fileName retrieved from database

问题描述

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);            
}

标签: angularasp.net-core-webapi

解决方案


推荐阅读