码迷,mamicode.com
首页 > Web开发 > 详细

jquery file upload without other plugin

时间:2015-05-20 09:57:26      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:jquery   ajax   fileupload   

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div>
    <form enctype="multipart/form-data">
    <input name="file" type="file" />
    <input type="button" value="Upload" />
</form>
<progress></progress>
</div>

<script>
$(':file').change(function(){
    var file = this.files[0];
    var name = file.name;
    var size = file.size;
    var type = file.type;
    //Your validation
});

    $(function(){
        $(':button').click(function(){
    var formData = new FormData($('form')[0]);
    $.ajax({
        url: '/excel',  //Server script to process data
        type: 'POST',
        xhr: function() {  // Custom XMLHttpRequest
            var myXhr = $.ajaxSettings.xhr();
            if(myXhr.upload){ // Check if upload property exists
                myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
            }
            return myXhr;
        },
        //Ajax events
//        beforeSend: beforeSendHandler,
//        success: completeHandler,
//        error: errorHandler,
        // Form data
        data: formData,
        //Options to tell jQuery not to process data or worry about content-type.
        cache: false,
        contentType: false,
        processData: false
    });
});
    });

    function progressHandlingFunction(e){
    if(e.lengthComputable){
        $('progress').attr({value:e.loaded,max:e.total});
    }
}
</script>
</body>
</html>


reference url: http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously

jquery file upload without other plugin

标签:jquery   ajax   fileupload   

原文地址:http://blog.csdn.net/u010102162/article/details/45850319

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!