开发者

Other ways to check the file size before upload

开发者 https://www.devze.com 2022-12-19 04:11 出处:网络
Is there any other way that I can just check the size of a file before upload?The r开发者_如何学Pythonequirement is if the file exceeded the limit, the form mustn\'t submit. If it\'s not, I have to do

Is there any other way that I can just check the size of a file before upload? The r开发者_如何学Pythonequirement 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.


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.


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号