locked
Seed Data Image RRS feed

  • Question

  • User-2132497554 posted

    Hi folks,

    I am using asp.net core. I want to save some photos into a database So I have created seed data for test.

    In model:

        public class Image
        {
            public int ImageId { get; set; }
            public string ImageName { get; set; }
            public string ContentType { get; set; }
            public byte[] Content { get; set; }
        }

    In SeedData:

        public static class SeedData
        {
            public static void Seed(this ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Image>().HasData(
                    new Image { ImageId=1, Content = "???" },
                    new Image { ImageId = 2, Content = "???" },
                    new Image { ImageId = 3, Content = "???" }
                    );
            }
        }

    I have saved three photos in webapplication(project) -> Images(Folder)->(cat1.png, cat2.png, cat3.png). 
    So how do i seed images?

    I am waiting for your response.

    Thanks in Advance!
    Ref:
    https://www.mikesdotnetting.com/article/259/asp-net-mvc-5-with-ef-6-working-with-files

    Thursday, July 4, 2019 7:45 AM

Answers

  • User-1764593085 posted

    Hi, lamGuy84,

    You could use the file path to open file in to a filestream and read data in a byte array.

    using System.IO;
    
    //...
    public static class SeedData
        {
            public static void Seed(this ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Image>().HasData(
    new Image { ImageId = 1, Content = ReadFile("images/Cat1.png") },
    new Image { ImageId = 2, Content = ReadFile("images/Cat2.png") },
    new Image { ImageId = 3, Content = ReadFile("images/Cat3.png") }
    ); } public static byte[] ReadFile(string sPath) { //Initialize byte array with a null value initially. byte[] data = null; //Use FileInfo object to get file size. FileInfo fInfo = new FileInfo(sPath); long numBytes = fInfo.Length; //Open FileStream to read file FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read); //Use BinaryReader to read file stream into byte array. BinaryReader br = new BinaryReader(fStream); //When you use BinaryReader, you need to supply number of bytes //to read from file. //In this case we want to read entire file. //So supplying total number of bytes. data = br.ReadBytes((int)numBytes); return data; } }

    Best Regards,

    Xing

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 5, 2019 6:33 AM
  • User753101303 posted

    Hi,

    You have also File.ReadAllBytes which read the file in one go and according to https://docs.microsoft.com/en-us/dotnet/api/system.io.file.readallbytes?view=netcore-2.2 it is available in ASP.NET Core.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 5, 2019 11:41 AM

All replies

  • User1289604957 posted

    Hi,

    Which version of ASP.NET Core are you using?

    Thank you.

    Best regards,

    Maher

    Thursday, July 4, 2019 1:45 PM
  • User-474980206 posted
    You just read the image files into byte arrays.
    Thursday, July 4, 2019 7:10 PM
  • User-2132497554 posted

    @maherjendoubi

    Thank you for your kind response.
    The version is 2.2

    Friday, July 5, 2019 4:04 AM
  • User-2132497554 posted

    @bruce

    Can you please show me the code?

    Thanks

    Friday, July 5, 2019 4:08 AM
  • User-1764593085 posted

    Hi, lamGuy84,

    You could use the file path to open file in to a filestream and read data in a byte array.

    using System.IO;
    
    //...
    public static class SeedData
        {
            public static void Seed(this ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Image>().HasData(
    new Image { ImageId = 1, Content = ReadFile("images/Cat1.png") },
    new Image { ImageId = 2, Content = ReadFile("images/Cat2.png") },
    new Image { ImageId = 3, Content = ReadFile("images/Cat3.png") }
    ); } public static byte[] ReadFile(string sPath) { //Initialize byte array with a null value initially. byte[] data = null; //Use FileInfo object to get file size. FileInfo fInfo = new FileInfo(sPath); long numBytes = fInfo.Length; //Open FileStream to read file FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read); //Use BinaryReader to read file stream into byte array. BinaryReader br = new BinaryReader(fStream); //When you use BinaryReader, you need to supply number of bytes //to read from file. //In this case we want to read entire file. //So supplying total number of bytes. data = br.ReadBytes((int)numBytes); return data; } }

    Best Regards,

    Xing

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 5, 2019 6:33 AM
  • User753101303 posted

    Hi,

    You have also File.ReadAllBytes which read the file in one go and according to https://docs.microsoft.com/en-us/dotnet/api/system.io.file.readallbytes?view=netcore-2.2 it is available in ASP.NET Core.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 5, 2019 11:41 AM