Determine file upload size before reading it all in Apache FileUpload Streaming API

There are two ways to use the Apache FileUpload library.

Fileupload

http://commons.apache.org/fileupload/using.html

And Streaming FileUpload API

http://commons.apache.org/fileupload/streaming.html

Both of them work just fine, except that the streaming API does not seem to be able to check the file file before processing the stream.

Is it because the Streaming API does not fundamentally know the file size or because I need to manually read some headers or something to get the Multipart download size?

+5
source share
1 answer

HTTP. HTTP , . , . HTTP TCP/IP. , , (.. - ).

JavaScript . HTML5 File API. Firefox, Chrome, Safari, Opera Android . IE9 , IE10.

<input type="file" ... onchange="checkFileSize(this)" />

checkFileSize() :

function checkFileSize(inputFile) {
    var max = 10 * 1024 * 1024; // 10MB

    if (inputFile.files && inputFile.files[0].size > max) {
        alert("File too large."); // Do your thing to handle the error.
        inputFile.value = null; // Clear the field.
    }
}
+14

All Articles