Wael Sayed

Wael Sayed

Friday, September 19, 2008

How to use Upload file control with UpdatePanal

In this Article we will Add upload file in a simple page works by ajax.Net.
Many developers faces a problems when add upload file control in an updatepanal control.Here is a simple code to avoid these problems In Html code add file upload control inside updatepanal then add PostBackTriggerAnd bind the upload button with the trigger as following:

In the Code behind, add the following lines of code:-
protected void Button1_Click(object sender, EventArgs e)
{
string filepath =Server.MapPath("~\\Upload");
HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i <>
{
HttpPostedFile userPostedFile = uploadedFiles[i];
if (userPostedFile.ContentLength > 0)
{
if ((userPostedFile.ContentLength / 1024) / 1024 > 4)
{
Label1.Text =
"Max Size";
}
else
{
userPostedFile.SaveAs(filepath + "\\" +System.IO.Path.GetFileName (userPostedFile.FileName));
}
}
}
}
-----------------------------------------------------------

1 comment:

Tamer Sayed said...

Well done, thanks for all these efforts.

Your brother
Tamer