Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have uploaded a file using the File Upload control in the webpage. I want to insert this into the database (MySQL). How do I achieve it? Can I do the same for inserting images as well?
Posted

 
Share this answer
 
SQL
For inserting image in to database we have to convert our image in fileupload in to byte array.
And make sure that our database column will support images. If not change the column type in to image type.


f (fuBrowse.PostedFile.ContentLength > 1) ----->1
{
FileUpload img = (FileUpload)fuBrowse;
Byte[] imgByte = null;
if (fuBrowse.HasFile && fuBrowse.PostedFile != null)
{

HttpPostedFile File = fuBrowse.PostedFile;---->2

imgByte = new Byte[File.ContentLength];--->3

File.InputStream.Read(imgByte, 0, File.ContentLength);


insertCommand.Parameters.AddWithValue("@image", imgByte);
insertCommand.ExecuteNonQuery();
}
}

1.Here we are checking the file upload contain file. fuBrowse is the file upload control.
2. Converting the file in fileupload in to HttpPostedFile
3.And we are converting this HttpPostedFile in to byte array.
4.Passing the byte array to database using commandName.Parameter.addWithValue.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900