我正在编写一个上传脚本,它将获取一个文件,对其进行处理,然后将其返回给用户。处理可能需要几分钟。我目前正在为用户提供反馈的操作是在实际上传期间显示上传栏,并在上传完成后将其替换为“处理”消息。文件完成处理后,该消息将替换为指向新文件的链接。这在 chrome 中运行良好,但在 Firefox 中,我只能将进度条调到 99.9%。有没有办法在关闭连接之前检查上传是否完成?
这是我目前拥有的
outter = $(".item_box")[0];//content wrapper
inner = outter.children[0];//content viewer, will be replaced with messages and content
var p = document.createElement("progress");
p.className = "uploadprogress";
p.value = "0";
p.max = "100";
inner.textContent = "";
inner.appendChild(p);//replace initial "click here" message with progress bar
var formData new FormData();
for (var i = 0; i < files.length; i++)//files is defined elsewhere and contains form data
{
formData.append('userfile', files[i]);
}
// now post a new XHR request
var xhr = new XMLHttpRequest();
xhr.open('POST', base_url+'upload/upload_media');//url of php script
xhr.responseType = "json";
xhr.onreadystatechange = function () {
if (xhr.readyState == 4)//ajax request complete
{
p.value = p.innerHTML = 100;//set progress bar to 100%
var data = JSON.parse(xhr.responseText);//my returned json object
outter.removeChild(inner);
var v = document.createElement("video");
v.controls = "controls";
v.innerHtml = data.msg;
outter.appendChild(v);
}
}
xhr.upload.onprogress = function (e)//handles progress bar value
{
if (e.lengthComputable)
{
var done = e.position || e.loaded, total = e.totalSize || e.total;
p.value = p.innerHTML = (Math.floor(done/total*1000)/10);
if (done == 100)//this never happens in firefox
{
inner.removeChild(p);
inner.textContent = "processing... please wait";
}
}
}
xhr.send(formData);