hi when i use fileupload control ,
i need to store the image serverside,
which is correct method store image to folder or database?
how to get the stored image in database to web page?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Suthish NairPosted Feb 28, 2011, 1:07 PM
Dorababu MekaPosted Feb 28, 2011, 5:27 AM
http://www.aspsnippets.com/Articles/Display-Images-in-GridView-Control-using-the-path-stored-in-SQL-Server-database.aspx
Marimuthu ArigovindasamyPosted Jun 30, 2010, 9:46 PM
protected void btnUpload_Click(object sender, EventArgs e)
{
byte[] imageData = null;
string fileName = string.Empty;
if (!string.IsNullOrEmpty(FileUpload1.FileName))
{
if (FileUpload1.HasFile)
{
imageData = FileUpload1.FileBytes;
fileName = FileUpload1.FileName;
}
}
string conString = @"Data Source=WVDI1IHCL32\WVDI1IHCL32;Initial Catalog=Test;Integrated Security=True;";
SqlConnection con = new SqlConnection(conString);
con.Open();
SqlCommand cmd;
string filename = FileUpload1.FileName;
cmd = new SqlCommand("insertingImage", con);
cmd.CommandText = "INSERT INTO ProductImage(ProductImage, Description) VALUES(@ProductImage ,@Description)";
cmd.Parameters.AddWithValue("@ProductImage", imageData);
cmd.Parameters.AddWithValue("@Description", "Sample image");
cmd.ExecuteNonQuery();
con.Close();
}
protected void btnRetrieve_Click(object sender, EventArgs e)
{
string conString = @"Data Source=WVDI1IHCL32\WVDI1IHCL32;Initial Catalog=Test;Integrated Security=True;";
SqlConnection con = new SqlConnection(conString);
con.Open();
SqlCommand cmd = null;
cmd = new SqlCommand("SelectImage", con);
cmd.CommandText = "Select * from ProductImage where ID = 1";
SqlDataReader sqlReader = cmd.ExecuteReader();
sqlReader.Read();
Byte[] imageData = (byte[])sqlReader["ProductImage"];
Response.BinaryWrite(imageData);
}
Database:
create table ProductImage
(
ID int not null identity constraint PK_Image primary key,
ProductImage image,
Description varchar(50)
)