gpt4 book ai didi

javascript - 使用 jquery 和 ajax 创建上传文件的进度条

转载 作者:行者123 更新时间:2023-11-29 15:45:14 26 4
gpt4 key购买 nike

我想使用 jquery 和 ajax 创建一个上传文件的进度条。所以我写了以下 jquery 代码。

function updateProgress(evt)
{
// evt is an ProgressEvent.
if (evt.lengthComputable)
{
var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
// Increase the progress bar length.
$(".progress > div").css(
{
width: percentLoaded + '%'
});

}
}

$.ajax(
{
url: 'assets/php/upload.php?action=uploadFiles',
type: 'POST',
data: newFormData,
cache: false,
xhr: function ()
{
myXhr = $.ajaxSettings.xhr();
if (myXhr.upload)
{
myXhr.upload.addEventListener('progress', updateProgress, false);
}
return myXhr;
},
contentType: false,
processData: false,
});

但问题是进度条会在一秒钟内100%,而上传文件还没有完成。我的代码有什么问题?

谢谢,

阿里雷萨

最佳答案

也许这与您的风格有关...不知道,因为我看不到您的所有代码。尝试以下。它正在工作,但对于小文件是察觉不到的。您可以使用断点或上传一个非常大的文件来查看它。来源:http://www.html5rocks.com/en/tutorials/file/dndfiles/

<style>
#progress_bar {
margin: 10px 0;
padding: 3px;
border: 1px solid #000;
font-size: 14px;
clear: both;
opacity: 1.0;
-moz-transition: opacity 1s linear;
-o-transition: opacity 1s linear;
-webkit-transition: opacity 1s linear;
}
#progress_bar.loading {
opacity: 1.0;
}
#progress_bar .percent {
background-color: #99ccff;
height: auto;
width: 0;
}
</style>

<input type="file" id="files" name="file" />
<button onclick="abortRead();">Cancel read</button>
<div id="progress_bar"><div class="percent">0%</div></div>

<script>
var reader;
var progress = document.querySelector('.percent');

function abortRead() {
reader.abort();
}

function errorHandler(evt) {
switch(evt.target.error.code) {
case evt.target.error.NOT_FOUND_ERR:
alert('File Not Found!');
break;
case evt.target.error.NOT_READABLE_ERR:
alert('File is not readable');
break;
case evt.target.error.ABORT_ERR:
break; // noop
default:
alert('An error occurred reading this file.');
};
}

function updateProgress(evt) {
// evt is an ProgressEvent.
if (evt.lengthComputable) {
var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
// Increase the progress bar length.
if (percentLoaded < 100) {
progress.style.width = percentLoaded + '%';
progress.textContent = percentLoaded + '%';
}
}
}

function handleFileSelect(evt) {
// Reset progress indicator on new file selection.
progress.style.width = '0%';
progress.textContent = '0%';

reader = new FileReader();
reader.onerror = errorHandler;
reader.onprogress = updateProgress;
reader.onabort = function(e) {
alert('File read cancelled');
};
reader.onloadstart = function(e) {
document.getElementById('progress_bar').className = 'loading';
};
reader.onload = function(e) {
// Ensure that the progress bar displays 100% at the end.
progress.style.width = '100%';
progress.textContent = '100%';
setTimeout("document.getElementById('progress_bar').className='';", 2000);
}

// Read in the image file as a binary string.
reader.readAsBinaryString(evt.target.files[0]);
}

document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>

关于javascript - 使用 jquery 和 ajax 创建上传文件的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12502482/

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