top of page

How to upload a file to a folder in ASP.NET Core MVC

Updated: Feb 28, 2023

In modern web applications, file uploads are an essential feature. In ASP.NET Core MVC, you can easily implement file-uploading functionality by following a few simple steps. In this article, we'll guide you through the process of uploading files to a folder in ASP.NET Core MVC.


Step 1: Create a new ASP.NET Core MVC Project

First, let's create a new ASP.NET Core MVC project. Open Visual Studio and create a new project. Select "ASP.NET Core Web Application" as the project type and choose the "Web Application (Model-View-Controller)" template. Name the project and click the "Create" button.


Step 2: Add a File Upload Form to the View

In the "Views" folder, find the view where you want to add the file upload form. Add the following code to the view:

<form method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label for="file">Choose file to upload:</label>
        <input type="file" name="file" id="file" class="form-control-file">
    </div>
    <button type="submit" class="btn btn-primary">Upload</button>
</form>

This creates a form with a file input field and a submit button. The "enctype" attribute is set to "multipart/form-data", which is necessary for uploading files.


Step 3: Add a File Upload Action to the Controller

In the controller for the view, add an action to handle the file upload:

[HttpPost]
public async Task<IActionResult> Upload(IFormFile file)
{
    if (file == null || file.Length == 0)
    {
        return Content("file not selected");
    }

    var path = Path.Combine(
                Directory.GetCurrentDirectory(), "wwwroot/uploads",
                file.FileName);

    using (var stream = new FileStream(path, FileMode.Create))
    {
        await file.CopyToAsync(stream);
    }

    return RedirectToAction("Index");
}

This action takes an IFormFile object as a parameter, which represents the uploaded file. The action checks if the file was actually selected, and if not, returns a message. If the file was selected, the action creates a path to save the file to, and then copies the file to that path using a FileStream. Finally, the action redirects the user back to the index page.


Step 4: Test the File Upload Functionality

Run the project and navigate to the view with the file upload form. Choose a file to upload and click the "Upload" button. The file should be uploaded to the specified folder and the user should be redirected back to the index page.


Conclusion:

In this tutorial, we have learned how to upload files to a folder in ASP.NET Core MVC. We created a new ASP.NET Core MVC project, added a file upload form to the view, added a file upload action to the controller, and tested the file upload functionality. This functionality can be used in many different applications, such as allowing users to upload images, documents, or videos to a website.


Frequently Asked Question


Q: What is a file upload form?

A: A file upload form is a type of form that allows users to select a file from their computer and upload it to a web server. It is typically used for uploading images, videos, documents, or other types of files.

Q: What is a file upload action in ASP.NET Core MVC?

A: A file upload action is a method in an ASP.NET Core MVC controller that handles the file upload functionality. It takes an IFormFile object as a parameter, which represents the uploaded file, and saves the file to a specified folder on the server.

Q: What is the enctype attribute in an HTML form?

A: The enctype attribute specifies the encoding type of the form data when it is submitted to the server. For file uploads, the enctype attribute should be set to "multipart/form-data", which allows the form data to contain files as well as regular form fields.

Q: Where are the uploaded files stored in ASP.NET Core MVC?

A: The uploaded files are typically stored in a folder on the server. In this tutorial, we used the "wwwroot/uploads" folder as the storage location, but you can choose any folder that is accessible to the application.

Q: Can multiple files be uploaded at once using ASP.NET Core MVC?

A: Yes, multiple files can be uploaded at once using ASP.NET Core MVC. You can modify the file upload form to include multiple file input fields, and modify the file upload action to handle multiple IFormFile objects.

0 comments
bottom of page