Ensure file size of uploaded file is lower than maxRequestLength before the upload occurs?
I've got a form with a FileUpload control where users can upload images. Can I ensure that the file size of the uploaded image is lower than the configured maxRequestLength before the upload occurs, and by doing so prevent an exception from being thrown when file size is too large?
My initial attempt of looking at ContentLength of PostedFile of the 开发者_C百科control was to no avail as the file is already submitted.
I have no interest in increasing the maxRequestLength.
From a usability point of view, a simple message telling people not to will go a long way to preventing the problem.
You could then try this (YMMV): It's only a hint to the browser, and is dependent on the browser supporting the syntax. The hidden field apparently needs to immediately precede your file input (or in asp.net, your File Upload control, check the markup in a browser to be sure).
<input type="hidden" name="MAX_FILE_SIZE" value="500" />
<input type="file" />
where the value is in Kb - so the above would limit to half a megabyte. Some browser's won't support this though, so you'll still need to check it on the server side, or accept that some people with odd/ancient/different browsers may still try to submit oversized files.
Found at this webpage and also this one
i just don't think this is possible within the confines of HTTP, you are going to have to use a flash uploader for this i believe. The only way for the server to know the size of the file is for the server to get the file
check out http://www.swfupload.org and the kitchen sink demo at http://demo.swfupload.org/v220/featuresdemo/index.php
It sounds like you have a requirement, "Prevent an exception," and a way that you think you should implement that goal, "Check the file size on the client side."
You can only 100% verify the file size on the client side by using a client-side technology, such as Flash or Silverlight. This prevents the exception, but forces you to tie yourself to a particular client-side technology.
You CAN prevent the exception, but it does occur after the file is uploaded (for the reasons you have stated before). Check this article for a code sample: http://aspnetresources.com/articles/dark_side_of_file_uploads
Similar functionality is provided out-of-the-box in a file upload component like NeatUpload (open source, LGPL): http://www.brettle.com/NeatUpload-1.3/dotnet/docs/Manual.html -> look at section 4.5.
It is possible to determine the size of the file that is on uploading even before the uploaded file has been transfered completely. Even when using Internet Explorer. By using the HTTP's content-length header!
In java world:
First of all the if you handle the request that arrive to the server in the first servlet/filter/controller before any previous thread mass with it, you'll be albe to get the total file size in the content-length header (request.getContentLength()).
second- have a look in an ajax implementation that prove that it is possible: http://www.ajaxfilebrowser.com/
精彩评论