6

Is there any other way that I can just check the size of a file before upload? The requirement is if the file exceeded the limit, the form mustn't submit. If it's not, I have to do the ordinary upload using the form and I don't have to exactly upload the file to the server using Flash.

jean27
  • 701
  • 8
  • 25

4 Answers4

2

Is there any other way that I can just check the size of a file before upload?

Not in JavaScript, the file size is not in the DOM.

Jonas Elfström
  • 30,834
  • 6
  • 70
  • 106
1

Checking the file size through the SWFUpload control is possible. Just put the SWFUpload control outside of the Web form tags. Tell the user click on the SWFUpload button and point to his upload file. Use javascript to determine the file size, then utilize this information as you see fit such as populating a validation function. Then your main form will need to ask the user to point to their upload file again, and it is this field which will do the actual uploading of the file. When the form is submitted, the SWFUpload control will be completely ignored since it's not part of the main form.

pwan
  • 2,894
  • 2
  • 24
  • 38
Jason M.
  • 11
  • 1
  • can you give an example? i too want just the filesize and nothing else, i have my own progress bar and my own way of uploading a file to my server. Basically i just want to retrieve the file size which IE can't give me yet. thx? – PathOfNeo Jan 14 '13 at 08:06
  • I think it is implied in the OP's question that the user shouldn't have to browse/select the file twice. – Amit Naidu May 13 '13 at 09:16
1

when instantiating SWFUpload, there are two parameters you need to pass: file_size_limit, and file_queue_error_handler:

new SWFUpload({
    file_size_limit: "10 MB",
    file_queue_error_handler: queueErrorHandler,
    [...]
})

and then:

function queueErrorHandler(file, errorCode) {
    if (errorCode == SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT) {
        alert("File exceeds the 10MB limit!");
    }
}

this checks if the file size is within limits before starting the upload

Victor Stanciu
  • 12,037
  • 3
  • 26
  • 34
  • 2
    I just don't want to upload the file using Flash. I mean, I just want to check the size of the file using SWFUpload and then upload the file using the normal process. Does it seem possible? – jean27 Feb 03 '10 at 08:23
  • I also found out that I can't manipulate the value of input type file so I guess what I really want is impossible. – jean27 Feb 03 '10 at 08:26
  • yes, using SWFUpload just to check the file size would work if you could set the value property of the normal file input – Victor Stanciu Feb 04 '10 at 10:36
  • We can't set the value property of the normal file input. It is illegal. – jean27 Feb 15 '10 at 09:02
  • @jean27, I'm trying to do exactly what you describe here, just check the file size with SWFUpload, and then let the normal form process everything else. Did you find a way to do it? Why do you say it is illegeal to "set the value property of the normal file input"? Thanks for any help. – TrojanName Jun 16 '11 at 12:21
  • @Brian, it's illegal to set the value property of the normal file input via Javascript and there's no HTML attribute for the to set the minimum file size. We just used SWFUpload for uploading files. We did not do the normal process. – jean27 Jun 21 '11 at 04:15
1

with the W3C FileAPI (implemented at least by Firefox 3.6) you can.

See this link for details

http://hacks.mozilla.org/2009/12/w3c-fileapi-in-firefox-3-6/

Cheers

denisjacquemin
  • 7,414
  • 10
  • 55
  • 72