Thursday, April 11, 2019

ASP.Net MVC file downloader to download file at user machine

Here is a simple source what will download file in user machine on a Asp.Net web application.
This can be implement in Angular application also.

Step 1 - new action in asp.net MVC controller


Byte[] dashboardfileBytes = File.ReadAllBytes(Full File LOCATION);

response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new ByteArrayContent(updatefilebytes);
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = repsourceFileInfo[0].Name;
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xlsm");


Step 2 - download from client side

download is very simple, you have a add a <a> tag and in href property you needs to send Asp.Net MVC url for the action you created above

ex:
<a hreff="mycontroller\FileDownloadAction" >download File </a>

the same logic you can implement in angular, instead of calling asp.net MVC controoler\action url, you can use WebAPI url.

No comments: