Uploading Images using ASP.NET

Hi,

This is an article cum code snippets for the persons who wants to upload photos using Asp.net Application,

Let we see step by 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 Going to Upload your Images.

2.Creating Button and DataGrid



The Second step is to Create a Button for Upload and a DataGrid control in the Asp.Net Application.

Then we have to create Stored Procedure for the Uploaded images,

3.Creating Procedure




//Execute the procedure with Microsoft.ApplicationBlocks.Data
CREATE PROCEDURE sp_img(@img image) AS

insert into tb_img values(@img)



4.Coding



The Final step is coding,

private void Button1_Click(object sender, System.EventArgs e)
{


if (Upload.PostedFile != null)
{


HttpPostedFile File = Upload.PostedFile;



byte[] Data = new Byte[File.ContentLength];
//force the control to load data in array

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

int i = 0;



for (i=0;i {
TextBox1.Text += Data[i].ToString();
}

//Create parameters for procedures

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;i {
//test your bitmap is valid

//Demonstration

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);

}
}


Like in the same way we can upload the Video files too.
Thanks,
Mani


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: