Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving multiple Images at once to the database with a FileUpload Control

I am working on a company Blog site, and when a user is making a post, they can add an image from their computer to the post. I used a FileUpload control to do this, and it works great. However, I am trying to change the functionality to allow the user to select and upload multiple images in one post, and I am running into some issues. I have set the 'allow multiple' property to 'true', however once multiple images are selected into the control (Image URLs get separated by a comma) and the post button is clicked, only one of the images is inserted into the database, but it is inserted as many times as images there are, and only that one image is displayed on the blog post. So if I try to add three different images, it inserts three instances of the first image into the database. The code corresponding to the the FileUpload in my onClick function is below:

  if (imageUpload.HasFile == true)
                {

                    SqlCommand maxMessId = new SqlCommand("SELECT Max(MessageID) FROM BlogMessages", conn);
                    lastMessageID = Convert.ToInt32(maxMessId.ExecuteScalar());

                    foreach (var uploadedFile in imageUpload.PostedFiles)
                    {



                        SqlCommand cmdInsertImage = new SqlCommand("INSERT INTO BlogImages(Image, MessageID) VALUES (@Image, @MessageID)", conn);

                        cmdInsertImage.Parameters.AddWithValue("@Image", SqlDbType.Image).Value = imageUpload.FileBytes;
                        cmdInsertImage.Parameters.AddWithValue("@MessageID", lastMessageID);


                        cmdInsertImage.ExecuteNonQuery();

                    }
                }

I am thinking the issue could be with:

cmdInsertImage.Parameters.AddWithValue("@Image", SqlDbType.Image).Value = imageUpload.FileBytes; 

If that is getting the file bytes for only one image.. I am not sure how to get the filebytes for both files. The image column in my BlogImages table is of the type Image.

Any suggestions are much appreciated!

like image 263
LeoStotch Avatar asked Feb 02 '26 13:02

LeoStotch


1 Answers

Are you sure that you're working on the right way ?????PostesFiles are the list of file and each one has it own properties you need to read from there.....nothing else

Here a simples examples

 For Each xx In fp.PostedFiles
        xx.InputStream
        xx.ContentLength
        xx.FileName


    Next

Where those properties upon expose Inputstrem a stream of the image,ContenteLenght it lenght, Filename the filename of the images.So when you pass the imageupload.FileBytes is not the correct way to achive your goal.You have to read the stream and return a bytearray to save the data withing your sql server.Nothing else i hope it could help you to solve your issue.

UPDATE*

Assume that you're into the foreach loop for each single file you have to setup its own bytearray

MemoryStream ms = new MemoryStream();
file.PostedFile.InputStream.CopyTo(ms);
var byts = ms.ToArray();
ms.Dispose();

then

change your insert statement like this

    cmdInsertImage.Parameters.AddWithValue("@Image", SqlDbType.Image).Value = byts;

not tested but it should solve the issue.

UPDATE 2

if (test.HasFiles) {

    StringBuilder sb = new StringBuilder();


    foreach (void el_loopVariable in test.PostedFiles) {
        el = el_loopVariable;
        sb.AppendLine("FILENAME:<B>" + el.FileName.ToString + "</B><BR/>");
        MemoryStream ms = new MemoryStream();
        el.InputStream.CopyTo(ms);
        byte[] byts = ms.ToArray;
        ms.Dispose();


        sb.AppendLine(string.Join(";", byts));
        sb.AppendLine("<br/<br/>");
        byts = null;
    }


    LitResponse.Text = sb.ToString;





}
like image 179
makemoney2010 Avatar answered Feb 05 '26 04:02

makemoney2010



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!