Download File From wwwroot Folder Asp.Net Core 10 Using C#

How to download file from wwwroot folder asp.net core 10 /asp.net core 6, 7,8,9/ mvc application using C#.net
In today's article I will show you a tutorial with example how you can download a file from wwwroot folder in ap.net core mvc 10 using c#.net. 

Here are few of the previous article to upload the file in wwwroot folder are as follows. Upload File in Asp.Net Core MVC Using C#.NetUpload File Outside of wwwroot Asp.Net Core MVC 8 in C#.NetUpload Image and Display Preview in Asp.Net Core 8 MVC Using C#.NetHow to Protect File for Direct Download in Asp.Net Core MVC, C#.NetSearch & Delete File from wwwroot in Asp.Net Core 8 MVC Using C#.NetHow to Create Text File in wwwroot in Asp.Net Core 8 MVC using C#.Net

So, for this article first we will create a new asp.net core mvc application and in wwwrooot folder we will add a text file which we are going to download. This file we will download on button click.

Text Test File

Now we will a controller and in this controller file we will add a httpget method. 
[HttpGet]
public IActionResult Index()
{
    return View();
} 
Now we will create a view and add the below code in it.
@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Click To Download File</h1>
    <a href="/Home/DownloadFile/" title="Download file"><b>DOWNLOAD</b></a>
</div> 
In above code I have defined a href path. In this I have defined an action method name responsible for downloading the specified file. Now lets check the code.
public IActionResult DownloadFile()
{
    var memory = DownloadSinghFile("TestTextFile.txt", "wwwroot");
    return File(memory.ToArray(), "text/plain", "TestTextFile.txt");
}
private MemoryStream DownloadSinghFile(string filename, string uploadPath)
{
    var path = Path.Combine(Directory.GetCurrentDirectory(), uploadPath, filename);
    var memory = new MemoryStream();
    if (System.IO.File.Exists(path))
    {
        var data = System.IO.File.ReadAllBytes(path);
        memory = new MemoryStream(data);
    }
    memory.Position = 0;
    return memory;
} 
Here we have created a method of name DownloadSinghFile of return type MemoryStream. Here we will read the file and return the memory stream of provided file. After that IActionResult will return the File type. Which make file to be downloaded. Here one thing to note. If you have a specific type of file you need the define the file type. Please check the list.
{ ".txt", "text/plain"},
{ ".pdf", "application/pdf"},
{ ".doc", "application/vnd.ms-word"},
{ ".docx", "application/vnd.ms-word"},
{ ".xls", "application/vnd.ms-excel"},
{ ".xlsx", "application/vnd.ms-excel"},
{ ".png", "image/png"},
{ ".jpg", "image/jpeg"},
{ ".jpeg", "image/jpeg"},
{ ".gif", "image/gif"},
{ ".csv", "text/csv"},
{ ".zip", "application/zip"} 
Now lets run the code to check the output.

Click to download file

Now click on Download link. You will be able to download the sample file. 

Downloaded Text file
Download File From wwwroot Folder Asp.Net Core 10 Using C#.zip 6KB

Post a Comment