使用AJAX和Bootstrap将文件上传到PHP的进度栏 [英] Progress Bar for file uploads to PHP using AJAX and Bootstrap

查看:67
本文介绍了使用AJAX和Bootstrap将文件上传到PHP的进度栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,可以使用AJAX将图像上传到PHP脚本

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

这是我的表格

<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>

结果div是显示PHP输出的位置(请参见AJAX)

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.

这是我的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;
        }); 
});

现在我得到一个输出,向我显示PHP中回显的数据.但是由于某种原因,我无法使进度条正常工作.

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.

可能是什么问题?

推荐答案

删除

async : false;

解决了.但是我现在需要看看如何重置标尺.

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

这篇关于使用AJAX和Bootstrap将文件上传到PHP的进度栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆