Download Files from Folder in .Net MVC

In ASP.Net MVC, downloading files from a folder is a common task that developers often encounter. Fortunately, it's relatively easy to accomplish this task using a few lines of code. In this blog post, we'll walk through the steps required to download files from a folder in ASP.Net MVC using C#.

Step 1: Create a new ASP.Net MVC Project
First, create a new ASP.Net MVC project in Visual Studio. You can do this by selecting "New Project" from the "File" menu, then selecting "ASP.Net Web Application" from the project types list.

Step 2: Create a folder to store the files
Next, create a folder in your project to store the files that you want to download. You can do this by right-clicking on your project in the Solution Explorer, selecting "Add", then selecting "New Folder".

Step 3: Add files to the folder
Once you've created the folder, you can add files to it by simply dragging and dropping them into the folder in the Solution Explorer.

Step 4: In the controller, create an action that retrieves the list of file names from the directory and passes them to the view:
public ActionResult FileList()
{
    var files = Directory.GetFiles(Server.MapPath("~/Downloads/"));
    List<string> fileNames = new List<string>();
    foreach (var file in files)
    {
        fileNames.Add(Path.GetFileName(file));
    }
    return View(fileNames);
}
In this example, the action retrieves a list of file paths using the Directory.GetFiles method and then extracts the file names using the Path.GetFileName method. The list of file names is then passed to the view using the View method.

In the view, loop through the list of file names and create a single ActionLink for each file:
@foreach (var fileName in Model)
{
    @Html.ActionLink(fileName, "Download", new { fileName = fileName })
    
}
In this example, the view uses a foreach loop to iterate over the list of file names passed from the action. For each file name, it creates an ActionLink using the Html.ActionLink helper method. The third parameter is an anonymous object that specifies the file name to download.

Step 5: Create a download action in the controller In the controller, create an action that will handle the file download. This action should take a file name as a parameter and return the file using the FileResult class. Here's an example of what the action might look like:
public ActionResult Download(string fileName)
{
    string filePath = Server.MapPath("~/Downloads/" + fileName);
    return File(filePath, "application/octet-stream", fileName);
}
In this example, the action takes a file name as a parameter and constructs a file path using the Server.MapPath method. It then returns the file using the FileResult class, specifying the MIME type as "application/octet-stream".

That's it! With these simple steps, you can download files from a folder in ASP.Net MVC using C#.
 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice