腾讯云Cos直传-任务成功确认

前面说,即使成功拿到了任务上传的cos地址,也不一定代表上传任务已经结束,图片已经存在于云端了,只是代表这个任务丢出去了。

拿cos的url地址反pin,可能会得到部分这样的结果,也无法顺利访问到存的图片:

 

怎么在前端确认我这个任务已经真的上传云端了呢,我的思路是:根据官方sdk中给的任务进度,直接显示在客户端上,上传任务进度达到100&后,再去反ping一下远程回传的cos地址,确认地址是有效地址。

首先在CosossService中挂一个委托事件,这样你在UI那边可以拿到上传任务的一个进度。

  public delegate void ProgressChangedHandler(double progress,string localfile,int taskid,string fileUrl);
  public event ProgressChangedHandler ProgressChanged;

//在上传任务的回调中,将事件挂上去
 uploadTask.progressCallback = delegate (long completed, long total)
 {
     double progress = completed * 100.0 / total;
     Console.WriteLine(String.Format("progress = {0:##.##}%", progress));
     // 触发进度更新事件
     ProgressChanged?.Invoke(progress, localFilePath, taskid, fileUrl);
 };

在UI那边监听这个事件

 cosossService = CosossService.CreateOssService();
 cosossService.ProgressChanged += Uploader_ProgressChanged;

   private void Uploader_ProgressChanged(double progress,string localfile, int taskid , string fileUrl)
   {
       // 更新进度条的值
       if (this.InvokeRequired)
       {
           this.Invoke(new Action(() =>
           {
              if(_tabledata != null && _tabledata.Count > 0)
               {
                   var finditem = _tabledata
                    .Select((item, idx) => new { Item = item, Index = idx })
                    .FirstOrDefault(x => x.Item.ID == taskid);
                   if (finditem != null)
                   {
                       var firstitem = finditem.Item;
                       if (firstitem != null)
                       {
                           string fileName1 = Path.GetFileName(localfile);
                           if (fileName1.Contains("first"))
                               firstitem.firstPageUploadProgress = progress;
                           if (fileName1.Contains("second"))
                               firstitem.secondPageUploadProgress = progress;
                       }
                       if (firstitem.firstPageUploadProgress > 99.999999)
                       {
                           if(IsUrlAccessible(fileUrl))
                               firstitem.isFirstpageUpload = 1;
                       }
                       if (firstitem.secondPageUploadProgress > 99.999999)
                       {
                           if (IsUrlAccessible(fileUrl))
                               firstitem.isSecondUpload = 1;
                       }
                       if(firstitem.isFirstpageUpload == 1 && firstitem.isSecondUpload == 1)
                       {
                           firstitem.isUpload = 1;
                           _dbUtil.UpdateFileUploadState(firstitem.ID);
                       }
                       _tabledata[finditem.Index] = firstitem;
                       this.dgTable_Refresh();
                   }
               }
           }));
       }
       else
       {
           if (_tabledata != null && _tabledata.Count > 0)
           {
               var finditem = _tabledata
                      .Select((item, idx) => new { Item = item, Index = idx })
                      .FirstOrDefault(x => x.Item.ID == taskid);
               if (finditem != null)
               {  
                   var firstitem = finditem.Item;
                   if (firstitem != null)
                   {
                       string fileName1 = Path.GetFileName(localfile);
                       if (fileName1.Contains("first"))
                           firstitem.firstPageUploadProgress = progress;
                       if (fileName1.Contains("second"))
                           firstitem.secondPageUploadProgress = progress;
                   }
                   if (firstitem.firstPageUploadProgress > 99.999999)
                   {
                       if (IsUrlAccessible(fileUrl))
                           firstitem.isFirstpageUpload = 1;
                   }
                   if (firstitem.secondPageUploadProgress > 99.999999)
                   {
                       if (IsUrlAccessible(fileUrl))
                           firstitem.isSecondUpload = 1;
                   }
                   if (firstitem.isFirstpageUpload == 1 && firstitem.isSecondUpload == 1)
                   {
                       firstitem.isUpload = 1;
                       _dbUtil.UpdateFileUploadState(firstitem.ID);
                   }
                   _tabledata[finditem.Index] = firstitem;
                   this.dgTable_Refresh();
               }
           }
       }
   }

在进度值达到100的时候,写一个方法去测试一下回传的url连通性:

 public static bool IsUrlAccessible(string url)
 {
     if (string.IsNullOrWhiteSpace(url))
     {
         throw new ArgumentException("URL不能为空", nameof(url));
     }

     try
     {
         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
         request.Method = "HEAD";
         using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
         {
             // 检查状态码是否为2xx(表示成功)
             return response.StatusCode == HttpStatusCode.OK;
         }
     }
     catch (WebException ex)
     {
         Console.WriteLine($"请求失败:{ex.Message}");
         return false;
     }
     catch (Exception ex)
     {
         Console.WriteLine($"发生未知错误:{ex.Message}");
         return false;
     }
 }

 

posted @ 2025-07-23 18:21  Wind_Swing_Dunn  阅读(26)  评论(0)    收藏  举报