Vb.net 如何暂停将文件上载到.Net中的Dropbox
我在Windows 10 32位上使用VB.Net和.Net Framework 4.6.2,我的文件几乎是12mb 我使用此代码将文件上载到DropboxVb.net 如何暂停将文件上载到.Net中的Dropbox,vb.net,file-upload,dropbox-api,pause,Vb.net,File Upload,Dropbox Api,Pause,我在Windows 10 32位上使用VB.Net和.Net Framework 4.6.2,我的文件几乎是12mb 我使用此代码将文件上载到Dropbox Public Async Function ChunkUpload(ByVal ThisFilePath As String, Progress1 As ToolStripProgressBar, Optional folder As String = ("
Public Async Function ChunkUpload(ByVal ThisFilePath As String, Progress1 As ToolStripProgressBar,
Optional folder As String = ("/Tests")) As Task
Dim config = New DropboxClientConfig("Project.vb")
Dim client = New DropboxClient(My.Settings.AccessToken, config)
Const chunkSize As Integer = 1024 * 1024
Dim LocalFileName As String = ThisFilePath.Remove(0, ThisFilePath.LastIndexOf("\") + 1)
Dim fs As IO.FileStream = New IO.FileStream(ThisFilePath, IO.FileMode.Open)
Dim data As Byte() = New Byte(fs.Length) {}
fs.Read(data, 0, data.Length)
fs.Close()
Try
Using thisstream = New IO.MemoryStream(data)
Dim numChunks As Integer = CType(Math.Ceiling((CType(thisstream.Length, Double) / chunkSize)), Integer)
Dim buffer() As Byte = New Byte((chunkSize) - 1) {}
Dim sessionId As String = Nothing
Dim idx = 0
Do While idx < numChunks
Dim byteRead = thisstream.Read(buffer, 0, chunkSize)
Dim memStream As IO.MemoryStream = New IO.MemoryStream(buffer, 0, byteRead)
If idx = 0 Then
Dim result = Await client.Files.UploadSessionStartAsync(body:=memStream)
sessionId = result.SessionId
Else
Dim cursor As Files.UploadSessionCursor = New Files.UploadSessionCursor(sessionId, CType((chunkSize * idx), ULong))
If idx = numChunks - 1 Then
'Overwrite, if existed
Await client.Files.UploadSessionFinishAsync(cursor,
New Files.CommitInfo(
(folder + ("/" + ThisFilePath)),
Files.WriteMode.Overwrite.Instance, False, Nothing, False), memStream)
Else
Await client.Files.UploadSessionAppendV2Async(cursor, body:=memStream)
End If
End If
idx += 1
Application.DoEvents()
Progress1.Value = CInt((idx / numChunks) * 100)
Progress1.ToolTipText = Progress1.Value & "%"
Loop
If Progress1.Value = 100 Then
Progress1.Value = 0
Progress1.ToolTipText = Progress1.Value & "%"
End If
End Using
Catch ex As DropboxException
MsgBox(ex.Message)
End Try
End Function
我的问题是:如何暂停/恢复/停止上传文件
更新(1):我发现这是关于上传/取消/暂停的,我会看看这种方法没有帮助
更新(2):我正试图按照@Greg的建议想出一种变通方法。Dropbox API“上载会话”功能是一种通过分段上传大文件的方法。该应用程序通过最初的调用为每个需要上传的大文件创建一个“上传会话”。每个上载会话的有效期为48小时
对的每个调用都可以包含一个连续的文件段
如果希望暂停上载会话,可以通过在对这三个方法的调用之间插入所需的任何流控制来完成。然后,只要您继续使用相同的
光标运行代码
,就可以在48小时内继续并完成上载会话 那么您是说它在编程上是可能的,但是Dropbox Api核心本身没有提供直接的方法来实现它?所以我需要想出一个解决方案?Dropbox.NET SDK不提供暂停/恢复单个请求的功能,但由于您控制单个请求的代码执行,您可以通过控制何时进行这些上载会话调用来暂停/恢复上载会话的整体操作。是的,我正在尝试按照您的建议使用总体代码,以想出一种技术。SessionID在尝试上传48小时后将返回Null
,对吗?您可能仍然有一个存储的SessionID
值,但是如果从最初检索到它已经48小时了,尝试在Dropbox API上使用它将失败。您的意思是“48小时后我不会得到异常,除非我真的启动了再发射?“。。。。如果是这样的话,那么好吧,我现在就去寻找另一种方法,仍然在你答案的框架内。
Private Async Sub DropboxToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DropboxToolStripMenuItem.Click
Dim DropThis As New Drobbox
Dim NewBakFile As String = "MYFILE.EXT" & Now.Date.ToShortDateString
Try
If ToolStripProgressBar1.Value <> 100 Then
Try
IO.File.Copy("ThisDB.accdb", NewBakFile, True)
Catch ex As IO.IOException
MsgBox("Error Copy : " & ex.Message)
End Try
ToolStripProgressBar1.Visible = True
BackupToolStripMenuItem.Enabled = False
'I obtain and store Access-Token here.
Await DropThis.ChunkUpload(NewBakFile, ToolStripProgressBar1)
BackupToolStripMenuItem.Enabled = True
ToolStripProgressBar1.Visible = False
DropLblUid.Text = ("Uploaded successfully. (" & Now.ToString("hh:mm:ss tt") & ")")
Try
IO.File.Delete(NewBakFile)
Catch ex As IO.IOException
MsgBox("Delete Error : " & ex.Message)
End Try
End If
Catch ex As IO.IOException
MsgBox(ex.Message)
Finally
DropboxToolStripMenuItem.Enabled = True
End Try
End Sub