Upload Image to ASP.NET Razor Page

Yusuf 681 Reputation points
2022-04-10T11:23:01.863+00:00

Hello,
How to add image upload in asp.net core razor pages to a Create and Edit page that is generated from:

Add > New Scaffolded Item > Razor Pages using Entity Framework (KRUD)

to save the name of image to database and upload image file to folder ?

Thanks in advance.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,153 questions
{count} vote

Accepted answer
  1. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2022-04-12T02:56:41.58+00:00

    Hi @Yusuf ,

    How to add image upload in asp.net core razor pages to a Create and Edit page that is generated from:

    Add > New Scaffolded Item > Razor Pages using Entity Framework (KRUD)

    to save the name of image to database and upload image file to folder ?

    You could refer the following sample:

    1.Assume you have create an AppFile class to store the upload file into the database:

    public class AppFile  
    {  
        public int Id { get; set; }  
        public string FileName { get; set; }  
        public byte[] Content { get; set; }  
    }  
    
    public class ApplicationDbContext : IdentityDbContext  
    {  
        public DbSet<AppFile> File { get; set; }  
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)  
            : base(options)  
        {  
        }  
    }  
    

    Enable migration and generate the database and data table, then use Scaffold to add the relates razor pages.

    2.To upload file in asp.net core, we need to use the IFormFile, so, create a FileViewModel, like this:

    public class FileViewModel  
    {   
        public IFormFile FormFile { get; set; }  
    }  
    

    3.Update the Create.cshtml.cs file as below:

    192144-image.png

    and modify the Create.cshtml page like this:

    192096-image.png

    You can view the source code from here: 192030-file-create.txt, more detail information about file upload, see Upload files in ASP.NET Core.

    Then, after running the application, the result like this:

    192112-1.gif


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Arnab 66 Reputation points
    2022-08-24T09:00:21.187+00:00

    Hi, I followed the same thing but I Am getting This Exception 234422-screenshot-2022-08-24-142513.jpg

    Can Anyone Suggest What I am doing wrong?

    0 comments No comments