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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting req.getContentLength() before whole file is uploaded 1

Status
Not open for further replies.

pr0n603

Programmer
Dec 14, 2004
23
US
Hi all,

I have a page that lets users upload files. I'm limiting users to uploading files that are smaller than 5MB. Currently, the server checks the HTTPServletRequest content length. However, this means that the whole file must 1st be uploaded before the servlet can respond with "file too large!" I want to be able to figure out the file size before the whole file is uploaded (i.e. the whole stream is read) and continue the upload if the file is small and report with an error if its too large.

are there any javascript methods or a way to use a servlet filter to get this done?

any ideas?

Thanks and happy holidays!
 
You don't have to read the stream in order to determine the upload file size as long as the Content-Length header is set ...

Code:
        int formDataLength = request.getContentLength();
        if (formDataLength > /* 5 Mb */(5*1024)) {
        	out.println("File " +((formDataLength/1024)/1024) +" Mb - too large !");
        	return;
        }

        // If get to here, then the file is OK, and you can start loading the file

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top