Wael Sayed

Wael Sayed

Saturday, September 20, 2008

How to set Custom Size for uploading

Sometimes we need to avoid large files from users, when upload photos, documents, mp3 files ...etc
In this article we validate files size to avoid users upload large size and customize a limit size for uploading files

First we need to add this lines in web.config file



So we change it to 200000
After Adding FileUpload control in the page add this code to button upload event



// Note : Custom size is 4 Mg


protected void Button1_Click(object sender, EventArgs e)
{

string filepath =Server.MapPath("~\\Upload");
HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i < userpostedfile =" uploadedFiles[i];"> 0)
{
if ((userPostedFile.ContentLength / 1024) / 1024 > 4)
{
Label1.Text = "Max Size";
}
else
{
userPostedFile.SaveAs(filepath + "\\" +
System.IO.Path.GetFileName(userPostedFile.FileName));
}
}
}


No comments: