Introduction This article is for beginners who want to learn how to upload images. Step 1 First add the following HTML tag :-
<input id="Upload" style="Z-INDEX: 102; LEFT: 104px; WIDTH: 288px; POSITION: absolute; TOP: 64px; HEIGHT: 22px" type="file" size="28" name="Upload" runat="server"> This control will upload your image. Step 2 Create a Button called "Upload" and another called "LoadImage". Add the DataGrid and do required bindings. Step 3 Lets see some sample code now.
private void Button1_Click(object sender, System.EventArgs e) { if (Upload.PostedFile != null) { HttpPostedFile File = Upload.PostedFile;
byte[] Data = new Byte[File.ContentLength]; File.InputStream.Read(Data,0,File.ContentLength); int i = 0;
for (i=0;i object[] obj = new object[1];
obj[0] = Data;
SqlHelper.ExecuteNonQuery(connectionString,"sp_img",obj);
} }
private void Button2_Click(object sender, System.EventArgs e) { DataSet ds = SqlHelper.ExecuteDataset(connectionString, "sp_load_img",null); grid.DataSource = ds.Tables[0].DefaultView; grid.ObjectName = "Image1"; grid.FieldName = "img"; grid.Editable = false; grid.DataBind(); grid.DataBindObjects(grid,ds,0,grid.PageSize);
int i =0;
for (i=0;i0].Rows.Count;i++) { byte[] bits = (byte[]) ds.Tables[0].Rows[i] ["img"]; MemoryStream memorybits = new MemoryStream(bits); Bitmap bitmap = new Bitmap(memorybits); }
}
private void grid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { grid.CurrentPageIndex =e.NewPageIndex; DataSet ds = SqlHelper.ExecuteDataset(connectionString, "sp_load_img",null); grid.DataSource = ds.Tables[0].DefaultView; grid.ObjectName = "Image1"; grid.FieldName = "img"; grid.Editable = false; grid.DataBind(); grid.DataBindObjects(grid,ds,e.NewPageIndex,grid.PageSize); } } Labels: ASP.Net, Image, image upload |