dpict99695329 2016-09-29 18:46 采纳率: 100%
浏览 281
已采纳

使用AJAX和Bootstrap将文件上传到PHP的进度条

I have a form with which I upload images using AJAX to a PHP Script

This is my form

<form action ="upload.php" method = "POST" enctype = "multipart/form-data" class = "form-horizontal" name="formData" id="data">
        <!--File Upload-->
            <div class = "form-group">
                <label class="control-label col-sm-1" for = "file">File:</label>    
                    <div class="col-sm-9">
                        <input type = "file" name = "image_file" id = "image_file"  class = "form-control" accept="image/*" onChange="autoPull(this.value)";>
                    </div>
            </div>
            <div class = "form-group">
                <label class="control-label col-sm-1" for = "project_name">ProjectName:</label>
                    <div class="col-sm-9">
                        <input type = "text" name ="project_name" id = "project_name" class = "form-control" placeholder="Enter Project Name"  value = "" required>
                    </div>
            </div>
            <div class = "button">
                <div class="form-group">
                    <div class="col-sm-offset-1 col-sm-6">
                        <input type="submit" name = "submit" class="btn btn-primary" value = "Submit" id="file_upload">
                        <input type="reset" name = "submit" class="btn btn-default" value = "Reset">
                    </div>
                </div>
            </div>
        </form>
        <br/>
            <div class="progress" style="display:none;">
              <div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width:0%;">

              </div>
            </div>
        <div id = "result"></div>

The result div is where the output from PHP is displayed(see in AJAX)

Progress bar is where I wish to see my bootstrap progress bar.

and this is my AJAX

$(function () {
        $('form#data').submit(function (e){
            e.preventDefault();
            e.stopImmediatePropagation();
            var formData = new FormData($(this)[0]);
            var file = $('input[type=file]')[0].files[0];
            formData.append('upload_file',file);
            $('.progress').show();
            $.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);
                            $('.progress-bar').css('width',percentComplete+"%");
                            $('.progress-bar').html(percentComplete+"%");
                            if (percentComplete === 100) {

                        }
                      }
                    }, false);
                    return xhr;
                  },
                type:'POST',
                url: 'upload.php',
                data: formData,
                async:false,
                cache:false,
                contentType: false,
                processData: false,
                success: function (returndata) {
                    $('#result').html(returndata);  
                }
            });
            return false;
        }); 
});

Now I get an output which shows me the data echoed in the PHP. But for some reason I cant get the progress bar to work.

What could be the issue?

  • 写回答

2条回答 默认 最新

  • duan1226 2016-09-29 19:37
    关注

    removing

    async : false;
    

    solved it. But I need to see how to reset the bar now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 Dkeil5 CT107D单片机的程序编写
  • ¥30 Ubuntu20.04中PVN3D复现过程交叉编译问题
  • ¥15 模拟电路求复阻抗和传递函数,请各位拍照写一下解答过程
  • ¥60 不懂得怎么运行下载来的代码
  • ¥15 CST导出3D模型图为什么和软件显示不一样?
  • ¥15 加热反应炉PLC控制系统设计(相关搜索:梯形图)
  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 经过滑动平均后的一维信号还原用什么结构好呢?
  • ¥15 指定IP电脑的访问设置
  • ¥30 matlab ode45 未发现警告,但是运行出错