Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Uploading file error. File size to big exception?? Not sure how to fix

Status
Not open for further replies.

LauraCairns

Programmer
Jul 26, 2004
173
0
0
GB
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);
}
}
 
Do you still want to allow the user to upload large files or do you want to enforce a maximum file size?

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Preferably i'd like to send an error message back to the user saying the file-size is too large however we've enough space in the DB and we could allow them to upload large file types too. Either of these solutions would be nice but the first one is what id prefer to do.
 
OK - if you want to increase the maximum file size you can do so in the we.config by setting the maxRequestLengthe.g.
Code:
<httpRuntime maxRequestLength="10000" />

If you want to inform the user about the file size you can check each uploaded file size by checking the e.g.
Code:
    Public Function FileFieldSelected(ByVal FileField As System.Web.UI.HtmlControls.HtmlInputFile) As Boolean
        ' Returns a True if the passed
        ' FileField has had a user post a file
        Dim intFileLength As Integer
        If FileField.PostedFile Is Nothing Then Return False
        If FileField.PostedFile.ContentLength = 0 Then Return False
        If FileField.PostedFile.ContentLength > 5120000 Then Return False
        Return True
    End Function
This is called like:
Code:
If FileFieldSelected(fileUpload1) = True Then ...

Hope this helps.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Could you tell me what actual file size this is. I would like to set a limit of 2MB on the file size. Have you any idea whay this would be.

If FileField.PostedFile.ContentLength > 5120000 Then Return False

Thanks so much for your help, much appreciated.
 
The file size is in bytes so you would just set the value to 2000000 (or 2097152 to be a bit more precise). If you want to do any conversions google is pretty good. i.e.



----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top