I am using .net Web API at my backend. There is a method for downloading file from the server. It is as:
[HttpGet]
[Route("recordFile")]
public BaseResponse RecordFile()
{
ViewModel data;
// ...something with data
string recordPath = data.RecordPath;
string fullpath = HttpContext.Current.Server.MapPath(recordPath);
string contentType = "application/" + Path.GetExtension(fullpath).Substring(1);
WebClient req = new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.ContentType = contentType;
response.AddHeader("Content-Disposition", "attachment;filename=\"" + fullpath + "\"");
response.BinaryWrite(req.DownloadData(fullpath));
response.End();
return OkRes(null, "Success", StatusCodeConstant.Ok);
}
When I hit the above method from the Postman, postman automatically opens the dialog to save the file.
But When using angular to download the file it return null data and the file does not get downloaded.
I am using this code in angular:
downloadRecordFile(): void {
this._HttpClient.get(`${this.apiRoot}/api/recordFile`).toPromise().then(data => {
console.log(data); // returns null
});
}
Any help would be appreciated.