gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:45:05 26 4
gpt4 key购买 nike

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

这是我的表格

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

进度条是我希望看到 Bootstrap 进度条的地方。

这是我的 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 中回显的数据。但由于某种原因我无法让进度条工作。

可能是什么问题?

最佳答案

这样就可以了

    var progress_bar = $(".progress-bar");
progress_bar.html("0%");
progress_bar.css("width", 0);
$(".progress").hide();

关于php - 使用 AJAX 和 Bootstrap 将文件上传到 PHP 的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39777837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com