简体   繁体   English

进度栏显示ajax,php中的加载百分比

[英]Progress bar displaying loading percentage in ajax,php

Hi i am using ajax for loading database content .I want to display the total percentage of loading or image. 嗨,我正在使用ajax加载数据库内容。我想显示加载或图像的总百分比。

This is my Script 这是我的剧本

function name1(str)
{
if (str.length==0)
 {
  document.getElementById("txtHint").innerHTML="";
   return;
 }
if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
 }
else
 {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
xmlhttp.onreadystatechange=function()
 {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
   document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
   }
 }
 xmlhttp.open("GET","user.php?q="+str,true);
xmlhttp.send();
}

user.php?q=ram , which is passing a value .And fetch the data from database How to modify ajax to to display the loading image user.php?q = ram,它传递一个值。然后从数据库中获取数据如何修改ajax以显示加载图像

there is no easyway to do this, as you can not know the real size of the response by javascript. 没有简便的方法可以执行此操作,因为您无法通过javascript知道响应的实际大小。

What you can do is: - make a first request to php, and have it send only the total bytes size of the image - then, inside your function name1(), you have to give the xmlhttp object a onprogress callback, HTML5 handles this: 您可以做的是:-向php发出第一个请求,并使其仅发送图像的总字节大小-然后,在函数name1()中,必须为xmlhttp对象提供一个进行中的回调,HTML5可以处理此问题:

var totalsize = //get it from a initial post request

xmlhttp.onprogress = function(response){
   var percentage = response.responseText.length / totalsize * 100;
   //update the progress bar, with that value...
};

then your onreadystatechange, handles the final chunk of data... 然后您的onreadystatechange处理最后的数据块...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM