I have an upload attachment page in my .NET application. It works fine as long as the uploaded file is smaller than 4 MB. How do I set the size of uploaded file? I'd like to set the limitation to be less than 8 MB. And if the file is larger than 8 MB, the upload process will be determined.
This is my code in the backend. File1 is the upload controler.
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
{
string name=ViewState["UserName"].ToString();
string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
string extension = Path.GetExtension(File1.PostedFile.FileName);
if (extension == "")
return;
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Attachments/" + name));
FileInfo[] rgFiles = di.GetFiles("*.*");
foreach (FileInfo fi in rgFiles)
{
if (fi.Name.Equals(fn))
{
ShowMessage(this, "This file name already exists, please check the list.");
return;
}
}
string SaveLocation = Server.MapPath("~/Attachments/" + name + "/" + fn);
try
{
File1.PostedFile.SaveAs(SaveLocation);
ShowMessage(this,"The file has been uploaded.");
}
catch (Exception ex)
{
ShowMessage(this,"Error" + ex.Message);
}
GetList();
}
else
{
ShowMessage(this,"Please choose one file");
}
}