如何暂停将文件上传到 .Net 中的 Dropbox
How can I pause uploading File to Dropbox in .Net
我在 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 = ("/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
我已经有另一个功能 returns Access-Token 并将其存储在 My.Settings.AccessToken
中。在我的表单中,我调用此函数来上传文件:
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
我的问题是:如何Pause/Resume/Stop上传文件?
更新(1) : 我找到了这个article关于Upload/Cancel/Pause,我去看看..... 那个方法没有帮助
更新 (2):我正在尝试按照@Greg 的建议想出一个解决方法。
Dropbox API "upload session" 功能是一种通过分段上传大文件的方式。该应用程序通过最初调用 UploadSessionStartAsync
为需要上传的每个大文件创建一个 "upload session"。每个上传会话的有效期为 48 小时。
对UploadSessionStartAsync
、UploadSessionAppendV2Async
、UploadSessionFinishAsync
的每次调用都可以包含一个连续的片段的文件。
如果您希望暂停上传会话,您可以通过在对这三种方法中的任何一种的调用之间插入您想要的任何流量控制来实现。只要您随后使用相同的 cursor
继续 运行 代码,在 48 小时内,您就可以继续并完成上传会话。
我在 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 = ("/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
我已经有另一个功能 returns Access-Token 并将其存储在 My.Settings.AccessToken
中。在我的表单中,我调用此函数来上传文件:
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
我的问题是:如何Pause/Resume/Stop上传文件?
更新(1) : 我找到了这个article关于Upload/Cancel/Pause,我去看看..... 那个方法没有帮助
更新 (2):我正在尝试按照@Greg 的建议想出一个解决方法。
Dropbox API "upload session" 功能是一种通过分段上传大文件的方式。该应用程序通过最初调用 UploadSessionStartAsync
为需要上传的每个大文件创建一个 "upload session"。每个上传会话的有效期为 48 小时。
对UploadSessionStartAsync
、UploadSessionAppendV2Async
、UploadSessionFinishAsync
的每次调用都可以包含一个连续的片段的文件。
如果您希望暂停上传会话,您可以通过在对这三种方法中的任何一种的调用之间插入您想要的任何流量控制来实现。只要您随后使用相同的 cursor
继续 运行 代码,在 48 小时内,您就可以继续并完成上传会话。