亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用jQuery的文件上傳進度條

使用jQuery的文件上傳進度條

UYOU 2019-07-08 16:20:56
使用jQuery的文件上傳進度條我正在嘗試在我的項目中實現Ajax文件上傳功能。為此,我使用jQuery;我的代碼使用Ajax提交數據。我還想實現一個文件上傳進度條。我該怎么做?有沒有方法來計算已經上傳了多少,這樣我就可以計算上傳的百分比并創建一個進度條了嗎?
查看完整描述

3 回答

?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

這個問題與jQuery表單插件..如果您正在尋找純jQuery解決方案,從這里開始..沒有針對所有瀏覽器的總體jQuery解決方案。所以你必須使用插件。我在用Drozone.js,對于較舊的瀏覽器來說,這是一個很容易的退路。你喜歡哪個插件取決于你的需求。外面有很多比較好的帖子。

實例:

jQuery:

$(function() {

    var bar = $('.bar');
    var percent = $('.percent');
    var status = $('#status');

    $('form').ajaxForm({
        beforeSend: function() {
            status.empty();
            var percentVal = '0%';
            bar.width(percentVal);
            percent.html(percentVal);
        },
        uploadProgress: function(event, position, total, percentComplete) {
            var percentVal = percentComplete + '%';
            bar.width(percentVal);
            percent.html(percentVal);
        },
        complete: function(xhr) {
            status.html(xhr.responseText);
        }
    });});

HTML:

<form action="file-echo2.php" method="post" enctype="multipart/form-data">
    <input type="file" name="myfile"><br>
    <input type="submit" value="Upload File to Server"></form><div class="progress">
    <div class="bar"></div >
    <div class="percent">0%</div ></div><div id="status"></div>

你必須用CSS來設計進度欄.。


查看完整回答
反對 回復 2019-07-08
?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

我只在jQuery中這樣做:

$.ajax({
  xhr: function() {
    var xhr = new window.XMLHttpRequest();

    xhr.upload.addEventListener("progress", function(evt) {
      if (evt.lengthComputable) {
        var percentComplete = evt.loaded / evt.total;
        percentComplete = parseInt(percentComplete * 100);
        console.log(percentComplete);

        if (percentComplete === 100) {

        }

      }
    }, false);

    return xhr;
  },
  url: posturlfile,
  type: "POST",
  data: JSON.stringify(fileuploaddata),
  contentType: "application/json",
  dataType: "json",
  success: function(result) {
    console.log(result);
  }});


查看完整回答
反對 回復 2019-07-08
?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

我在我的項目中使用了以下內容。你也可以試試。

ajax = new XMLHttpRequest();ajax.onreadystatechange = function () {

    if (ajax.status) {

        if (ajax.status == 200 && (ajax.readyState == 4)){
            //To do tasks if any, when upload is completed
        }
    }}ajax.upload.addEventListener("progress", function (event) {

    var percent = (event.loaded / event.total) * 100;
    //**percent** variable can be used for modifying the length of your progress bar.
    console.log(percent);});ajax.open("POST", 'your file upload link', true);ajax.send(formData);//ajax.send is for uploading form data.


查看完整回答
反對 回復 2019-07-08
  • 3 回答
  • 0 關注
  • 728 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號