.Net 5

Upload Image/Video/File using Form Data .Net 6 | .Net 5

.Net 6 provides a very simple way to Upload Image, Video or any type of File. I do receive emails that people are facing issue while writing Web API for uploading any File Type using .Net 6 or any previous version e.g. .Net 5 or .Net Core.

There are more than one way of uploading Files using ASP.Net Core or .Net Web API. We can also get bytes stream, convert into image or save in wwwroot folder. The other way of doing this is even more simpler. When we receive Image or File using Form Data then It’s really simple to save that file.

Uploading Image in Web API Controller

First of all we need to create images folder inside wwwroot folder.

Here’s our code.

[HttpPost]
public string UploadImage([FromForm]IFormFile file)
{
    try
    {
        // getting file original name
        string FileName = file.FileName;

        // combining GUID to create unique name before saving in wwwroot
        string uniqueFileName = Guid.NewGuid().ToString() + "_" + FileName;

        // getting full path inside wwwroot/images
        var imagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images/", FileName);
        
        // copying file
        file.CopyTo(new FileStream(imagePath, FileMode.Create));

        return "File Uploaded Successfully";
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}

How to Use WWWROOT Directory

If you don’t have wwwroot directory in your project, it’s really simple to add.

Just add the below line in your Configure Action of your project’s Startup.cs File.

app.UseStaticFiles();

Now create a new Folder as wwwroot at the root of your project.

Testing File Upload using Postman

We need to select form-data parameter to upload image/file. We’ll select POST method because we have written a POST Web Service. Make sure that you are sending file with the exact name that you are receiving file in your API.

You can see that File has been successfully Uploaded.

If you want to send some more parameters with File, Here’s your solution.

Uploading Image using Model in Web API Controller

Sometimes we need to send some other parameters with file as well. For achieving this we need to create a custom Model. Here’s our Model

using System;
using Microsoft.AspNetCore.Http;

public class UserModel
{
    public int Id { get; set; }

    public string Name { get; set; }
    public string Address { get; set; }
    public IFormFile Image { get; set; }
}

Now we can USE this Model instead of single File.

[HttpPost(nameof(UploadImage))]
public string UploadImage([FromForm]UserModel data)
{
}

You can access your data using data object, e.g. data.Name, data.Address, data.Image

That’s all for now. Thank you for reading the article. If you have any question, Please ask in the comment section below.

Author

I'm passionate about learning new technologies as well as mentoring and helping others get started with their programming career. This blog is my way of giving back to the Community.

Write A Comment