1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] Save asp:Image ImageUrl as byte[]? to SQL database VARBINARY(MAX)

Discussão em 'Outras Linguagens' iniciado por Stack, Outubro 9, 2024 às 22:32.

  1. Stack

    Stack Membro Participativo

    I have a page that displays an ASP:Image using C#.

    Once the image is displayed, now how do I update the database, saving the ASP:Image into VARBINARY(MAX).

    example, if there is no image in the database upon loading, a default image is read from a file and put into the ASP:Image object. Now when I go to update the record, I need to save the ASP:Image into the database.

    I have multiple options, after searching. The one that made the most sense was this:

    if(img_user_image.ImageUrl.Length > 0)
    {
    byte[] bytes = Convert.FromBase64String(img_user_image.ImageUrl.Replace("data:image/png;base64,", ""));
    cmd.Parameters.Add("@user_image", System.Data.SqlDbType.VarBinary, -1).Value = bytes.ToArray();
    }



    I have tried removing the base64 header and setting bytes to be an array. But I am not saving the ASP:Image to the database.

    I am not "just uploading" the file so using the FileUpload methods don't help. I figure I need to convert to a MemoryStream, but how do I get the ASP:Image into the memory stream? Everything I tried, failed.

    When loading the ASP:Image object from a file upload, it works like a champ

    if(fileUpload1.HasFile)
    {
    intFileSize = fileUpload1.PostedFile.ContentLength;
    byte[] input = new byte[intFileSize - 1];
    input = FileUpload1.FileBytes;
    img_postedFile.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(input.ToArray(), 0, input.ToArray().Length);
    }


    Where img_postedFile is an ASP:Image object. And saving from the uploaded image is easy peezy too!

    SQL = "INSERT INTO tblImages (Name, Size, ImageData) " +
    "VALUES (@file_name,@file_size,@image_file)";
    cs = DBAccess.get_cs();
    SqlCommand cmd = new SqlCommand(SQL, new SqlConnection(cs));
    SqlDataAdapter da = new SqlDataAdapter(SQL, cmd.Connection);
    cmd.Parameters.Add(new SqlParameter("@file_name", FileUpload1.FileName));
    cmd.Parameters.Add(new SqlParameter("@file_size", intFileSize));
    cmd.Parameters.Add(new SqlParameter("@image_file", input));
    cmd.Connection.Open();
    int i = cmd.ExecuteNonQuery();
    cmd.Connection.Close();



    But that's not what I need to do. Now I need to save that ASP image file to the database, later in processing and not while uploading.

    Googling has me going in circles and not getting to a solution. Surely there must be a way (possibly pathetically simple) to read a displayed ASP:Image and store it to a SQL database.

    I'm hoping I'm just not asking the question correctly. Help!

    Thanks in Advance.

    if(img_user_image.ImageUrl.Length > 0)
    {
    byte[] bytes = Convert.FromBase64String(img_user_image.ImageUrl);
    cmd.Parameters.Add("@user_image", System.Data.SqlDbType.VarBinary, -1).Value = bytes;
    }


    Look at the initial post, it has a couple different attempts.

    Continue reading...

Compartilhe esta Página