File Upload Validation RRS feed

  • Question

  • User838651582 posted

    Hi !

    I have built a forum where people can upload pictures.  Webmatrix block the server for download over 4 Mo but it shows a page as there is a problem in the code.  I would like to put a validation to the form that can send back the user on the page with a red error message under the file upload input area.  I have tried this code but it doesn't work:

    var fileBytes = fileUpload.GetBytes();
        Validator.Range(1,1000000, "Maximum of 1Mo per picture and 4Mo for the total of your pictures"));

    Someone have an idea?

    Thursday, May 9, 2013 3:34 PM

All replies

  • User-1270160193 posted

    This is a bit tangential to your question, but can you upload bigger files if you increase the value of the maxRequestLength property in your web.config file or does WebMatrix prevent you from changing that?



    1. I don't know if it makes any difference, but fileBytes.Length seems to be mispelled as fileBytes.Lenght in the above example.
    2. Modern Browsers like IE10 seem to have a new feature that supports checking file length on the client: http://stackoverflow.com/questions/307679/using-jquery-restricting-file-size-before-uploading
      (You could check the file length in jQuery using the above trick and possibly (alert the user)/(cancel the operation) *before* the file is ever submitted to the server.  You should note, however, that client side validation like this should *always* be followed up with server side validation)
    Thursday, May 9, 2013 3:40 PM
  • User-821857111 posted

    The Validation helpers don't work with the Request.Files collection. They only work with Request.Form. If you exceed the maximum request length (default is 4MB), you will cause an HttpException to be raised if the Request object is referenced. The best way to handle that is to add some code to the Application_Error event in global.asax. Otherwise you can increase the maxrequest length in your web.config.  But even doing that will not solve the problem if an upload exceeds your new limit. And uploads exceeding 26MB or so are blocked by default by IIS before ASP.NET even gets a sniff at objecting.

    Thursday, May 9, 2013 4:05 PM
  • User838651582 posted

    All the files under 4Mo are OK.  I didn't try to increase the maxRequestLength but thanks for the info, I did'nt know that we could change those settings.  If there is nothing in the Web.config file is it because I'm on the regular settings?

    Thursday, May 9, 2013 8:28 PM
  • User838651582 posted

    Thanks Mike, it looks to be the right way to handle it.  But I'm not sure of all the things I have to do.  I have found a bit of information and they said that:

     I should create 2 pages and place them at the root.  I have to create a page AppEvent.aspx with this code:

    <%@ Page Language="C#" %>
    <script runat="server">
    	void Page_Load(object sender, System.EventArgs e)
    		throw(new ArgumentNullException());

    And another page Global.asax with the code:

    void Application_Error(object sender, EventArgs e) 
            // Code that runs when an unhandled error occurs
    	    Exception objErr = Server.GetLastError().GetBaseException();
    	    string err =	"Error Caught in Application_Error event\n" +
    			    "Error in: " + Request.Url.ToString() +
    			    "\nError Message:" + objErr.Message.ToString()+ 
    			    "\nStack Trace:" + objErr.StackTrace.ToString();
    	    //additional actions...


    1) When I do that, I have an error that indicates that EventLog doesn't exist?

    2) And I will probably have to indicates to redirect to my new page with a nice message... but where to add it?

    3) Do I have to specify somewhere that I want to catch only the error for the upload over 4Mo?

    Thanks in advance,

    Thursday, May 9, 2013 9:15 PM
  • User-1482720155 posted


    Monday, July 8, 2013 6:20 AM
  • User1526116210 posted

    The Application_Error event handler handles all application errors which is good if you want to display friendly error messages to your users. You should only need to edit your Global.asax file and then create a page to display your friendly error messages.

    1. Try not to overcomplicate things, this is all I have in my Application_Error handler in my Global.asax and then my error page is a static html page:
          void Application_Error(object sender, EventArgs e) 
    2. You can't indicate the redirect before it happens, it is best just to have your message on the error page
    3. I don't think it is possible to only catch the maximum request length exception using this method. It could be possible using a try-catch block somewhere but I have not been successful with this.

    Tuesday, July 9, 2013 2:29 AM
  • User838651582 posted

    I have modified the MaxRequestLength in the webconfig and it works for the first part of my problem (to accept larger files), but when I arrive to the MaxRequestLength it doesn't work to sjow the error page.  I have put your code in a Global.asax like this:

    <%@ Application Language="C#" %>
    <script runat="server">
        void Application_Error(object sender, EventArgs e) 
            // Code that runs when an unhandled error occurs

    But it doesn't redirect to the error page.  Do I have to add something in my page with the file upload form?

    Tuesday, July 9, 2013 10:53 PM