dougezhua0017 2015-02-26 17:30
浏览 36
已采纳

在php中显示加载百分比文本

I'm new here and I'm sorry if i have posted this in the wrong place or anything.

What i want to implement is a text which displays percentage from 1%-100% which shows how much a file has been uploaded.

Right now, It shows just "Loading.." Instead of uploading..

I'm using PHP and JS in the website. Here is the script for the "Loading" button.

    echo "<form id=\"uploads\" action=\"\" method=\"post\" enctype=\"multipart/form-data\"><input type=\"hidden\" value=\"myForm\" name=\"$upload_name\">
<center><input type=\"file\" value=\"Upload\" name=\"upload_file[]\" class=\"file\" multiple class=\"button2\"/> <br><br><input type=\"button\" id=\"upload\" class=\"button2\" value=\"Upload\" onclick=\"document.getElementById('upload').disabled = 'disabled'; document.getElementById('upload').value = 'Loading...'; document.getElementById('uploads').submit();\"><br><br></center>
</form></center>
";

How can i implement this? Please direct me to the path where i can implement this feature.

  • 写回答

1条回答 默认 最新

  • douba7784 2015-02-26 18:26
    关注

    So a "without library" solution. Provide the URL of your server upload handler, select your file and click on upload. You should see the progression as a percentage.

    document.querySelector("#upload").addEventListener("click",function(){
      
       var oReq = new XMLHttpRequest();
    
       oReq.addEventListener("progress", updateProgress, false);
       oReq.addEventListener("load", transferComplete, false);
       oReq.addEventListener("error", transferFailed, false);
       oReq.addEventListener("abort", transferCanceled, false);
    
      
       var upload_to_URL= document.querySelector("#upload_to").value;
       oReq.open('POST', upload_to_URL , true);
      
      
       var formData = new FormData();
       formData.append("upload", file.files[0]);
       
       oReq.send(formData);
    
    });
    
    
    // progress on transfers from the server to the client (downloads)
    function updateProgress (oEvent) {
      if (oEvent.lengthComputable) {
        var percentComplete = oEvent.loaded / oEvent.total;
        // ...
        document.querySelector("#upload-progress").innerHTML= (percentComplete * 100 ) + "%"
      } else {
        // Unable to compute progress information since the total size is unknown
      }
    }
    
    function transferComplete(evt) {
        document.querySelector("#upload-progress").innerHTML= " <span style='color:green'>100%</span>";
    }
    
    function transferFailed(evt) {
      alert("An error occurred while transferring the file.");
    }
    
    function transferCanceled(evt) {
      alert("The transfer has been canceled by the user.");
    }
    <form id="upload-form">
      <input type="text" id="upload_to" placeholder="Upload handler URL"/><br />
      <input type="file" id="file"/>
      <input type="submit" value="upload" id="upload" />
    </form>
    <div id="upload-progress"></div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R