2017-04-24 13 views
4

をダウンロード:GoogleドライブV3再開可能私はGoogleドライブのAPI V3を使用してGoogleドライブからファイルをダウンロードするには、このコードを使用

Function gDownload() As Boolean 

      Dim fileID As String = "0B-W1aRTTOB1QM3hPd0dOUFVObHM" 
      Dim stream = New System.IO.MemoryStream()   
      Dim r = service.Files.Get(fileID) 
      Dim m = r.MediaDownloader 
      m.ChunkSize = 256 * 1024 
      AddHandler m.ProgressChanged, AddressOf Download_ProgressChanged  
    mStart:  
      r.Download(stream)  
      Return True ' or False if download failed 

    End Function 

    Private Sub Download_ProgressChanged(s As IDownloadProgress) 
      Console.WriteLine(s.Status.ToString & " " & s.BytesDownloaded) 
    End Sub 

これは、安定した接続で正常に動作しますが、私が接続を失った場合、それはあっても、永遠に停止し、待機私は再びつながる。

私は更新(アップロード)でこの問題を持っていない、このコード内の関数:

Function gUpload() As Boolean 

     Dim fileID As String = "0B-W1aRTTOB1QM3hPd0dOUFVObHM" 
     Dim stream As New System.IO.FileStream("D:\gtest\Test.mp4", System.IO.FileMode.Open) 
     Dim fBody As File = New File With {.Name = "Test.mp4"} 

     Dim r = service.Files.Update(fBody, fileID, stream, "application/octet-stream") 
     r.ChunkSize = ResumableUpload.MinimumChunkSize 
     AddHandler r.ProgressChanged, AddressOf Upload_ProgressChanged 

    mStart: 
     r.Resume() 
     If r.GetProgress.Status = 3 Then ' UploadStatus.Completed 
      Return True 
     Else 
      If MessageBox.Show("Failed. do you want to resume?", "Failed", MessageBoxButtons.YesNo) = DialogResult.Yes Then 
       GoTo mStart 
      End If 
     End If 
     Return False 
End Function  

Private Sub Upload_ProgressChanged(s As IUploadProgress) 
    Console.WriteLine(s.Status.ToString & " " & s.BytesSent) 
End Sub 

これは、私はいくつかの時間(15〜30代)のための接続を失った場合、私は、望むとおりに正確に動作し、それが与えますアップロードが失敗したというメッセージと、ユーザーに再開または終了の選択肢が与えられます。すべてが完璧に機能します。

私の質問は、ダウンロード機能をアップロード機能のように動作させる方法、または少なくとも永遠に待たずに失敗メッセージを出す方法です。

答えて

1

私は同じ問題を抱えていた、私は

Request.DownloadAsync(Stream) 

    Dim bytes_downloaded As Long = 0 
    Dim not_downloaded As Integer = 0 
    Dim progress As Integer = Stream.Length 
    Do While progress < file_size 
     progress = Stream.Length 

     If progress = bytes_downloaded Then 
      not_downloaded += 1 
      If not_downloaded >= 900 Then '90 seconds 
       ' msg download falhou 
       Exit Do 
      End If 
     Else 
      not_downloaded = 0 
     End If 
     bytes_downloaded = progress 
     Threading.Thread.Sleep(100) 
    Loop 
+0

ありがとう、私はこの問題の回避策として非常に似たようなことをしましたが、私はGoogleドライブがより良い方法を持っていると確信しています。 – Fadi

1

私はあなたがtihsのようなあなたのDownload_ProgressChanged手順を変更すべきだと思う:

Private Sub Download_ProgressChanged(s As IDownloadProgress) 
    Select Case s.Status 
    Case DownloadStatus.Downloading 
     If True Then 
      Console.WriteLine(progress.BytesDownloaded) 
      Exit Select 
     End If 
    Case DownloadStatus.Completed 
     If True Then 
      Console.WriteLine("Download complete.") 
      Exit Select 
     End If 
    Case DownloadStatus.Failed 
     If True Then 
      Console.WriteLine("Download failed.") 
      Exit Select 
     End If 
    End Select 
End Sub 

リンクはこちらです:https://developers.google.com/drive/v3/web/manage-downloads

部分ダウンロードbuを使用すると、開始バイトと終了バイトを計算する必要があります。

+0

ありがとう、しかし 's.Status'は常に「DownloadStatus.Downloading'あると' Failed'または 'Completed'になることはありません「しばらく」で一時的に解決しました。 – Fadi

関連する問題