Ajax提交進度顯示執行個體

來源:互聯網
上載者:User

標籤:setting   nload   src   dstar   上傳   url   jquery   data   erro   

概述:ajax提交比較大的檔案的時候,我們希望能夠看到它上傳的進度,代碼放下面了。

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>XMLHttpRequest 上傳檔案進度條樣本</title></head><body><progress id="upload_progress" value="0" max="100"></progress><input id="upload_file" type="file" name="upload_file" /><button id="btn_start">Start</button><button id="btn_cancel">Cancel</button><script type="text/javascript" src="/res/lib/jquery-1.11.3.js"></script><script type="text/javascript">var xhr = new XMLHttpRequest();var progressBar = document.getElementById(‘upload_progress‘);$(‘#btn_start‘).click(function() {    var uploadFile = document.getElementById(‘upload_file‘).files[0];    var formData = new FormData();    formData.append(‘upload_file‘, uploadFile);    // ---------------------------------------    // 原生xmlHttpRequest發送    xhr.open(‘post‘, ‘/server_url.php‘);    xhr.onload = function() {        alert(‘完成!‘);    };    xhr.onerror = function() {        alert(‘無法串連伺服器!‘);    };    xhr.upload.onprogress = function(progress) {        if (progress.lengthComputable) {            console.log(progress.loaded / progress.total * 100);            progressBar.max = progress.total;            progressBar.value = progress.loaded;        }    };    xhr.upload.onloadstart = function() {        console.log(‘started...‘);    };    xhr.send(formData);    // ---------------------------------------    // 使用ajax發送    /**    $.ajax({       type: "POST",      url: "/server_url.php",      data: formData , //這裡上傳的資料使用了formData 對象      processData: false,       contentType: false, //必須false才會自動加上正確的Content-Type      //這裡我們先拿到jQuery產生的 XMLHttpRequest對象,為其增加 progress 事件綁定,然後再返回交給ajax使用      xhr: function() {        var xhr = $.ajaxSettings.xhr();        if (xhr.upload) {          xhr.upload.onprogress = function(progress) {                    if (progress.lengthComputable) {                        console.log(progress.loaded / progress.total * 100);                        progressBar.max = progress.total;                        progressBar.value = progress.loaded;                    }                };                xhr.upload.onloadstart = function() {                    console.log(‘started...‘);                };         }              return xhr;       }    }).done(function(resp) {        alert(‘完成!‘);    }).fail(function(err) {        alert(‘無法串連伺服器!‘)    });*/});$(‘#btn_cancel‘).click(function() {    xhr.abort();});</script></body></html>

原理就是這樣,需要在此基礎上做擴充的可以隨意添加自己想要的功能。

Ajax提交進度顯示執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.