随笔 - 103,  文章 - 0,  评论 - 1,  阅读 - 16万
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>进度条属性上传</title>
</head>
<body>
    <form method="post" enctype="multipart/form-data">
      <input name="key"  type="hidden" value=""/>
      <input name="token"  type="hidden" value=""/>
      <input name="accept" type="hidden" />
      <input id="input-file" class="upload" type="file" value="" onchange="getPhoto(this)">
         <br/>
         <span class="img"> </span>

         <div id="totalBar" style="float:left;width:20%;height:30px;border:1px solid;border-radius:3px">
            <div id="totalBarColor" style="width:0%;border:0;background:#FFFE33; color: #FFF;height:28px;"></div>
            <span class="speed"></span>
         </div>

         <br/>
         <BR/>
      <input type="button" value="上传" onclick="upload()"/>
      <input type="button" value="暂停" onclick="filepause()"/>
    </form>
     <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
     <script src="https://unpkg.com/qiniu-js@2.5.4/dist/qiniu.min.js"></script>



        <script>
                    var subObject;
                    var file;
                    //定义上传配置,注意上传域名的设置,华东空间z0,华南z2,华北z1
                    var config = {
                      region: qiniu.region.z1
                    };


                    var putExtra = {

                    };

                    var compareChunks = [];
                    var observable;
                    var subscription;

                    function getPhoto(node) {
                        var imgURL = "";
                        try{
                            file = null;
                            if(node.files && node.files[0] ){
                                file = node.files[0];
                            }else if(node.files && node.files.item(0)) {
                                file = node.files.item(0);
                            }
                        }catch(e){

                        }
                        creatImg(imgURL,file.name);
                        return imgURL;
                    }

                    function creatImg(imgRUL,fileName){
                        $("input[name=key]").val(fileName);
                        //var textHtml = "<img src='"+imgRUL+"'width='40px' height='40px'/>";
                        //$(".img").html(textHtml);
                        $("#totalBarColor").css("width","0%");
                    }


                  function upload() {
                            // 设置next,error,complete对应的操作,分别处理相应的进度信息,错误信息,以及完成后的操作
                        subObject = {
                              next: next,
                              error: error,
                              complete: complete
                        };
                        token = "填写你们的上传token"
                        //上传
                        observable = qiniu.upload(file, file.name, token, putExtra, config);
                        // 调用sdk上传接口获得相应的observable,控制上传和暂停
                        subscription = observable.subscribe(subObject);
                    }

                    function filepause(){
                       subscription.unsubscribe();
                    }

                    //分片上传返回response,标记上传进度
                    var next = function(response) {
                       var chunks = response.chunks||[];
                       var total = response.total;
                       $(".speed").text("进度:" + total.percent + "% ");
                       $("#totalBarColor")
                           .css(
                                "width",
                                 total.percent + "%"
                       );
                       compareChunks = chunks;
                     };

                     var error = function(err) {
                           alert(err.message);
                     };

                     var complete = function(res) {
                         console.log(res);
                         console.log(res.key);


                     };
            </script>
</body>

</html>
posted on   独醉笑清风  阅读(160)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示