Home > How & Why, Interactive knowledge & Tips n Tricks & other reference stuff > How to stop a user to upload big size files?

How to stop a user to upload big size files?

February 9th, 2011 589 views Leave a comment Go to comments

If you are planning to validate a file over its size at client side only using some java script then SORRY.
File upload

Listen

Listen

Listen

You need not to be disappointed. I have many solutions

  1. ActiveX control
    Write the following code in script tag in your HTML.

    function getSize()
    {
    	var myFSO = new ActiveXObject("Scripting.FileSystemObject");
    	var filepath = document.upload.file.value;
    	var thefile = myFSO.getFile(filepath);
    	var size = thefile.size;
    	alert(size + " bytes");
    }
    

    Please note this
    Use of ActiveX control is always avoided due to security reasons.
  2. Java Applet – I had written a Java applet. But i’ll prefer to write a separate code for the same.
  3. SWF – I haven’t tried it before. But it is a very good option.
  4. .htaccess
    Write following line in your .htaccess file.

    LimitRequestBody 2097152

    Apache error log will generate this entry when you exceed this limit on a form post or get request:

    Requested content-length of 4000107 is larger than the configured limit of 2097152
    

    And it will also display this message back in the web browser:

    <h1>Request Entity Too Large</h1>

    By the way, the error number returned is 413. So, you could use a directive in your .htaccess file.

    Redirect 413 413.html
  5. Best way:
  6. Let client upload the file. Don’t write it at server end immediately. Instead,

    1. Create a file progress bar who monitors how much part of a file has been uploaded.
    2. Once it crosses maximum specified size limit;
      1. Leave writing
      2. Prompt the client.

    Now see how to implement this logic in Java

    1

    Download following jars

    • commons-fileupload-1.2.1
    • commons-io-1.4

    You will get them easily on apache sites.


    2

    Add following code in your java class say servlet.

    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.*;
    
    import org.apache.commons.io.*;


    3

    Following line will help you to identify whether client is uploaded a file or not.

    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    


    4

    If yes then first read all the values through some iterator. And check whether there is any non form field (means whether the current item is a file)

    Object img = itr.next();
    FileItem item = (FileItem) img;
    if (!item.isFormField()){
    :
    }
    


    5

    Now its up to you whether you write a file on your server immediately. Or Byte by Byte. I’ll suggest for second option.

    byte[] boundary = "article-stack.net".getBytes();
    //byte[] boundary = new byte[1000000]; //geting hanged by defining fixed no of bytes
    try{
        MultipartStream ms = new MultipartStream(item.getInputStream(),boundary,1000);
    
        FileOutputStream fileOut = new FileOutputStream(savedFile);
        ms.readBodyData(fileOut);
        fileOut.flush();
        fileOut.close();
    }catch(Exception exp)
    {
      savedFile.delete();
      exp.printStackTrace();
    }
    


    6

    Its not done boss. You have to write a progress listener.

    ProgressListener progressListener = new ProgressListener(){
       private long megaBytes = -1;
       public void update(long pBytesRead, long pContentLength, int pItems) {
           long mBytes = pBytesRead / 1000000;
           if (megaBytes == mBytes) {
               return;
           }
           megaBytes = mBytes;
           System.out.println("We are currently reading item " + pItems);
           if (pContentLength == -1) {
               System.out.println("So far, " + pBytesRead + " bytes have been read.");
           } else {
               System.out.println("So far, " + pBytesRead + " of " + pContentLength
                                  + " bytes have been read.");
           }
       }
    };
    

    Modify the above progress listener as per your need to limit the file size.

Happy……..

Amit Gupta

Hey! this is Amit Gupta (amty). By profession, I am a Software Eng. And teaching is my passion. Sometimes I am a teacher, as you can see many technical tutorials on my site, sometimes I am a poet, And sometime just a friend of friends...

  1. July 8th, 2011 at 02:27 | #1

    NOT WHOLE, only up to the threshold value.
    If you are creating a progress listener which is watching the file how much it is uploaded then you can throw an exception once it crosses threshold size.
    Otherwise you would have to use some client side application like SWF,Applet. I already had mentioned it in article.

  2. user
    July 5th, 2011 at 19:33 | #2

    the key is how to “STOP” uploading. your Java solution responses to the size limit after uploading the whole content of the large files.

  3. prathamesh
    February 9th, 2011 at 20:08 | #3

    Good post.