It may be 3 years late, but I wanted to share the solution here for future reference.
Firstly, in your ASP.NET web API controller:-
[Route("downloadFile")]
[HttpGet]
public HttpResponseMessage DownloadFile()
{
HttpResponseMessage response = null;
try
{
response = new HttpResponseMessage(HttpStatusCode.OK);
// Specify the file path - if serving from App_Data folder, adjust as needed
string filePath = HttpContext.Current.Server.MapPath("~/App_Data");
response.Content = new StreamContent(new FileStream($"{filePath}\\app.exe", FileMode.Open, FileAccess.Read));
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "app.exe";
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/exe");
return response;
}
catch(Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
}
}
As shown, the process of sending a .exe file is similar to sending other file types like .txt/.pdf. Just make sure to set the correct ContentType
value: application/XXX
.
You can access this API endpoint from your Angular SPA by using HttpClient or include the API URL in the href
attribute of an a
tag:
<a href="http://localhost:XXXXX/url/to/downloadFile">Download</a>
Clicking on this link will trigger a GET request to the specified URL, resulting in the file download.