0

I have a SQL Server table with a varbinary(max) column. I use it to store images in it.

VariantImage    varbinary(MAX)  Checked

Then an image is stored in the database using this code:

public async Task<IActionResult> PartVariant(PartVariant model, IFormFile files)
 {
     if (ModelState.IsValid)
     {
        DynamicParameters look = new DynamicParameters();

        foreach (var file in Request.Form.Files)
        {
            MemoryStream ms = new MemoryStream();
            file.CopyTo(ms);
            model.VariantImage = ms.ToArray();
            ms.Close();
            ms.Dispose();
            look.Add("@VariantImage", model.VariantImage);
        }

        string employee = _db.ExecuteReturnScalar<string>("usp_InsertPartVariant", look);
        TempData["Success"] = " Part Variant Added Successfully!";
        return RedirectToAction("PartVariant", "MasterViewData");
    }
}

It looks like this stored in the database:

0xFFD8FFE000104A46494600010100000100010000FFDB0043000403030403030404040405050405070B07070606070E0A0A080B100E1111100E100F12141A16121318130F10161F171..........................etc

I am unable to load the images back from the datatable.

Controller and model class:

public IActionResult PartVariant()
{
    Part_Bind();
    viewLookup look = new viewLookup();
    var model = look.GetPartVariant(_db, connectionString);
    TempData["partVariant"] = model;
    return View();
}


public List<PartVariant> GetPartVariant(IDapperORM _db, string connection)
{
    SqlConnection conn = new SqlConnection(connection);
    List<PartVariant> MasterList = new List<PartVariant>();

    SqlCommand com = new SqlCommand("GetPartVariant", conn);
    com.CommandType = CommandType.StoredProcedure;

    SqlDataAdapter da = new SqlDataAdapter(com);
    DataTable dt = new DataTable();

    conn.Open();
    da.Fill(dt);
    conn.Close();

    foreach (DataRow dr in dt.Rows)
    {
        MasterList.Add(
            new PartVariant
            {
                VariantImage = (byte[])dr["VariantImage"],
            }
        );
    }

    return MasterList;
}

I'm getting this error:

enter image description here

Ivin Raj
  • 3,448
  • 2
  • 28
  • 65
  • 1
    *I use it to store images in it* - side note that your computer already has a really good database for files, that makes things a bit easier to work with.. For example, IIS already knows how to serve files out of a filesystem - you just put the path in the html and when the browser requests it IIS streams it off disk and down the socket. It doesn't need you to write any code that burns server resources doing the trivial task of writing bytes to a socket – Caius Jard Jan 04 '22 at 06:32
  • can you help me. guide me i will change the code@CaiusJard – Ivin Raj Jan 04 '22 at 06:36
  • 1
    I'm saying you should ideally take your images out of the DB and put them on disk in some folder that IIS is permitted to access. Life becomes very simple then and you don't bloat your DB with megabytes of images. Putting them in the DB is like keeping them in a zip file; max pain, little gain – Caius Jard Jan 04 '22 at 06:37
  • Seeing the code of the stored procedures and the table definition would be handy. Somehow your image has become a string(?) but nothing in this code posted so far appears to do that conversion – Caius Jard Jan 04 '22 at 06:38
  • How is this different from your previous question https://stackoverflow.com/questions/70435045/sql-server-data-type-var-binary-max-to-convert-image-to-display-using-asp-net-mv – Charlieface Jan 04 '22 at 09:49
  • This link can guide you. File upload in ASP.NET Core MVC (Storing in Folder) https://tutexchange.com/file-upload-in-asp-net-core-mvc-storing-in-folder/ – Saineshwar Bageri - MVP Jan 07 '22 at 17:15

0 Answers0