Vb.net 谷歌硬盘V3可恢复下载
我使用以下代码使用Google Drive api V3从Google Drive下载文件:Vb.net 谷歌硬盘V3可恢复下载,vb.net,google-drive-api,Vb.net,Google Drive Api,我使用以下代码使用Google Drive api V3从Google Drive下载文件: Function gDownload() As Boolean Dim fileID As String = "0B-W1aRTTOB1QM3hPd0dOUFVObHM" Dim stream = New System.IO.MemoryStream() Dim r = service.Files.Get(
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秒),它会给出一个消息,上传失败,并让用户选择继续或退出。一切都很好
所以我的问题是:如何让下载功能像上传功能一样工作,或者至少让它不会永远等待并发出失败消息。我认为您应该更改下载过程更改程序,如tihs:
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
链接在这里:
你可以使用部分下载,但你必须计算开始和结束字节。我也有同样的问题,我用“while”暂时解决了
Request.downloadsync(流)
Dim字节\u下载的长度=0
Dim未下载为整数=0
将进度调整为整数=Stream.Length
进行时执行<文件大小
进度=流长度
如果进度=字节\u下载,则
未下载+=1
如果未下载>=900,则“90秒”
"下载发愁"
退出Do
如果结束
其他的
未下载=0
如果结束
下载的字节数=进度
线程。线程。睡眠(100)
环
谢谢,但是s.Status
总是“DownloadStatus.Downloading”并且永远不会成为失败的
或完成的
。谢谢,我做了非常类似的事情来解决这个问题,但我相信Google Drive有更好的方法,我们找不到它。
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