LauraCairns
Programmer
I have a strange exception being thrown in a web application im working on to do with upload files. Everything has been working ok but as soon as it went into system testing we've discovered that we haven't handled an exception for whenever the file size is too big. Unforunately when we try to upload a file which is greater than 5MB then it falls over without even going into the upload click event. I'm a bit stuck on how to handle this error and what to do. Is anyone able to help me or point me the right direction. THe click event to upload the event is as follows: -
CODE FOR UPLOADING
private void imgSave_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
try
{
lblErrorMessage.Text = "";
string sExtension = "";
HttpPostedFile myFile = null;
if (this.FileFieldSelected(this.fileToUpload))
{
myFile = fileToUpload.PostedFile;
string sValidTypes = "";
if ( UIUtilities.IsValidFileType(myFile.FileName,ref sExtension, ref sValidTypes))
{
int nFileLen = myFile.ContentLength;
byte[] myData = new byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
(myFile.FileName);
string sFilename = this.txtFileName.Text;
string sUserId = Session["UserID"].ToString();
if(Request.QueryString["FORMTYPE"] == "1")
{
Employer objEmployer = BusObjFactory.GetUsersEmployer(sUserId);
long lFileId = objEmployer.AddApplicationForm(sFilename,
sExtension, ref myData);
}
else
{
Employer objEmployer = BusObjFactory.GetUsersEmployer(sUserId);
long lFileId = objEmployer.AddMonitoringForm(sFilename,
sExtension, ref myData);
}
Response.Redirect("AppFormsMaintain.aspx");
}
else
{
this.lblErrorMessage.Text = "Invalid file type. Valid type are " + sValidTypes;
}
}
}
catch(Exception ex)
{
this.DisplayErrorMessage("Error saving form: " + ex.Message);
Log.Error("Exception in UploadForms.aspx on Save event" + ex.Message);
}
}
CODE FOR UPLOADING
private void imgSave_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
try
{
lblErrorMessage.Text = "";
string sExtension = "";
HttpPostedFile myFile = null;
if (this.FileFieldSelected(this.fileToUpload))
{
myFile = fileToUpload.PostedFile;
string sValidTypes = "";
if ( UIUtilities.IsValidFileType(myFile.FileName,ref sExtension, ref sValidTypes))
{
int nFileLen = myFile.ContentLength;
byte[] myData = new byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
(myFile.FileName);
string sFilename = this.txtFileName.Text;
string sUserId = Session["UserID"].ToString();
if(Request.QueryString["FORMTYPE"] == "1")
{
Employer objEmployer = BusObjFactory.GetUsersEmployer(sUserId);
long lFileId = objEmployer.AddApplicationForm(sFilename,
sExtension, ref myData);
}
else
{
Employer objEmployer = BusObjFactory.GetUsersEmployer(sUserId);
long lFileId = objEmployer.AddMonitoringForm(sFilename,
sExtension, ref myData);
}
Response.Redirect("AppFormsMaintain.aspx");
}
else
{
this.lblErrorMessage.Text = "Invalid file type. Valid type are " + sValidTypes;
}
}
}
catch(Exception ex)
{
this.DisplayErrorMessage("Error saving form: " + ex.Message);
Log.Error("Exception in UploadForms.aspx on Save event" + ex.Message);
}
}