我使用此代码使用Google Drive 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~30s),它会发出上传失败的消息,并让用户可以选择恢复或退出.一切都很完美.
所以我的问题是:如何让下载功能像上传功能一样工作,或者至少使它不会永远等待,并给出一个失败的消息.
最佳答案 我有同样的问题,我暂时解决了“同时”
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