Check (Validate) File (Image) Size before upload using JavaScript and jQuery

Check (Validate) File (Image) Size before upload using JavaScript and jQuery

 20 Nov 2014   Mudassar Khan
 1 Comments     19019 Views
  JavaScript    jQuery    jQuery Plugins    HTML5

Here Mudassar Ahmed Khan has explained how to check (validate) File (Image) size before upload using JavaScript and jQuery.

HTML5 allows developers to access the file contents and details using JavaScript and jQuery and hence in browsers that support HTML5 one can easily determine the size of the File.
For browsers that don’t support HTML5 have to use jQuery Flash plugins like Uploadify.

Don't forget to download Aspose as well: http://aspose.com/file-tools
In this article I will explain how to check (validate) File (Image) size before upload using JavaScript and jQuery.
HTML5 allows developers to access the file contents and details using JavaScript and jQuery and hence in browsers that support HTML5 one can easily determine the size of the File.
For browsers that don’t support HTML5 have to use jQuery Flash plugins like Uploadify.
 
Determining the size of the file using JavaScript and HTML5
The following HTML markup consists of an HTML FileUpload and a Button. When the button is clicked, a function named Upload is executed.
Inside this function, first a check is performed to verify whether the browser supports HTML5 File API. If the browser supports HTML5 File API then the size of the file is determined and the displayed.
< input  type="file" id="fileUpload" />
< input  type="button" value="Upload" onclick="Upload()" />
< script  type="text/javascript">
    function Upload() {
        var fileUpload = document.getElementById("fileUpload");
        if (typeof (fileUpload.files) != "undefined") {
            var size = parseFloat(fileUpload.files[0].size / 1024).toFixed(2);
            alert(size + " KB.");
        } else {
            alert("This browser does not support HTML5.");
        }
    }
</ script >
 
 
Determining the size of the file using jQuery and HTML5
The following HTML markup consists of an HTML FileUpload and a Button. When the button is clicked, a jQuery click event handler is executed.
Inside the click event handler, first a check is performed to verify whether the browser supports HTML5 File API. If the browser supports HTML5 File API then the size of the file is determined and the displayed.
< input  type="file" id="fileUpload" />
< input  type="button" id="upload" value="Upload" />
< script  type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
< script  type="text/javascript">
    $(function () {
        $("#upload").bind("click"function () {
            if (typeof ($("#fileUpload")[0].files) != "undefined") {
                var size = parseFloat($("#fileUpload")[0].files[0].size / 1024).toFixed(2);
                alert(size + " KB.");
            } else {
                alert("This browser does not support HTML5.");
            }
        });
    });
</ script >
 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值