js上传进度条

<script type="text/javascript">
    $(function () {
        DemoForm.init('${empty cmsInfo.infoId ? seq:cmsInfo.infoId}','${cmsModule.moduleId}');
        
        $("#submit").click(function(){


        $('#videoURL').html("");
        $(".progress-bar").width('0%');
        $(".progress").show();
        var formData = new FormData(); 
        formData.append("file", document.getElementById('file').files[0]); 
        //formData.append("token", token_value); 

        $.ajax({ 
            url: "/ClouldCms/cms/CmsVideo/insertVideo.action",
            type: "POST", 
            data: formData, 
            dataType : "json",
            processData: false, // 不要对data参数进行序列化处理,默认为true
            contentType: false, // 不要设置Content-Type请求头,因为文件数据是以 multipart/form-data 来编码
            xhr: function(){
                myXhr = $.ajaxSettings.xhr();
                if(myXhr.upload){
                  myXhr.upload.addEventListener('progress',function(e) {
                    if (e.lengthComputable) {
                      var percent = Math.floor(e.loaded/e.total*100);
                      if(percent <= 100) {
                     
                    //  alert(percent);
                       // $("#J_progress_bar").progress('set progress', percent);
                       // $("#J_progress_label").html('已上传:'+percent+'%');
                     $(".progress-bar").width(percent+'%');
                       
                      }
                      //if(percent >= 100) {
                        //$("#J_progress_label").html('文件上传完毕,请等待...');
                        //$("#J_progress_label").addClass('success');
                      //}
                    }
                  }, false);
                }
                return myXhr;
            },
            success: function(res){ 
      //alert(res.flag);
            if(res.flag){
      //alert("123123");
            var r = $("#videoPath").val(res.data);
      //alert("上传视频成功");
      $(".progress").hide();
      $('#videoURL').html('<video  id="example_video_1" class="video-js vjs-default-skin" controls preload="none" width="500" height="300" data-setup="{}"><source  src="' + basePath + "/" + res.data + '" type="video/mp4" /></video>');


      }
            },
            error: function(res) {
                // 请求失败
                console.log(res);
            }
            
        }); 
       }); 
        //上传视频
       /**    $("#submit").click(function(){
      var formData = new FormData();
      formData.append('file', $('#file')[0].files[0]);
    $.ajax({    
            url : "/ClouldCms/cms/CmsVideo/insertVideo.action",    
            type : "POST",    
            data : formData,
            dataType : "json",
            cache: false,
            processData: false,
            contentType: false,
      success:function(res) {
      if(res.flag){
      var r = $("#videoPath").val(res.data);
      alert("上传视频成功"+res.data);
      $('#videoURL').html('<video  id="example_video_1" class="video-js vjs-default-skin" controls preload="none" width="500" height="300" data-setup="{}"><source  src="' + basePath + "/" + res.data + '" type="video/mp4" /></video>');


      }
      }
    });*/
     
     
     


       /* $("#submit").click(function(){
     ajaxFileUpload({
     url:"/ClouldCms/cms/CmsVideo/insertVideo.action",
     secureuri:false,
     fileElement:"file",
     dataType:"json",
     success:function(data){
     alert(data);
     }
     });
       });  */
        //截图,处理图片
        $("#videoPic").on('click',function(){
           // createIMG();
            var scale = 0.5,
            video = $('#videoURL').find('video')[0],
            canvas = document.createElement("canvas"),
      canvasFill = canvas.getContext('2d');
            canvas.width = video.videoWidth * scale;
            canvas.height = video.videoHeight * scale;
canvasFill.drawImage(video, 0, 0, canvas.width, canvas.height);
var src = canvas.toDataURL("image/jpeg");
            $('#imgSmallView').html('<img src="' + src + '" alt="预览图" width="250" height="150" />');
            
            src = src.replace(/\+/g,"%2B");
            //var imageDate = src.substring(23);
           $.ajax({
            url:"/ClouldCms/cms/CmsVideo/ImagePath.action",
            type:"POST",
            data:"data="+src,
            dataType:"json",
            success:function(res){
            if(res.flag){
          $("#imgPath").val(res.data);
          alert("返回图片路径");
            }
            }
            });
        });
          
    });
  
</script>
 

猜你喜欢

转载自my.oschina.net/u/3362233/blog/1649051