weixin_33696822 2016-02-05 22:04 采纳率: 0%
浏览 48

使用进度条上传文件

I create this code to upload file usin ajax and php and I want to add progress bar to show percent of upload.

this my code

<script>
   $("form#data").submit(function(){
    var formData = new FormData($(this)[0]);
    $.ajax({
        url: "functions/video.php",
        type: 'POST',
        data: formData,
        async: false,
        success: function (data) {
            document.getElementById("status").innerHTML = data;
        },
        cache: false,
        contentType: false,
        processData: false
    });
    return false;
});
</script>
<form id="data" method="post" enctype="multipart/form-data">
<input name="up_vid" type="file" id="up_vid"/>
<div class="upload_v_icon"></div>
<div class="video_info">
    <input type="text" name="video_title" placeholder="Video title" />
    <input type="text" name="tags" placeholder="funny,9gag,nice,crazy ..."/>
    <textarea name="description" placeholder="Description"></textarea>
    </div>
    <div class="bg_upload">
    <p>When you upload this video your are agree with <a href="">Terms</a> of service.</p>
    <button>Begin Upload</button>
    </div>
</form>

Thank you.

  • 写回答

1条回答

  • lrony* 2016-02-05 22:16
    关注

    This assumes a 1px wide gif called "progress.gif" is present. Set the color of this to the color you want your progress bar to appear.

    Add something like this to your css:

    .uploadBar {
       background-image:url(/images/progress.gif);
       background-position: -1px;
       background-repeat:no-repeat;
       background-size=0% 100%; width:100%;
       position: relative; overflow: hidden;
    }
    

    Add something like this to your $.ajax();

    xhr: function() {
       var xhr = new window.XMLHttpRequest();
       //Upload progress
       xhr.upload.addEventListener("progress", function(evt) {
          if (evt.lengthComputable) {
          var percentComplete = evt.loaded / evt.total;
          console.log(percentComplete);
          $('.uploadBar').css({ backgroundSize: (percentComplete*100) + '%'});
          }
       }, false);
       return xhr;
       }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮