我在Windows 10 32bit上将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
我已经有另一个函数返回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
我的问题是:如何暂停/继续/停止上传文件?
Update(1):我已经找到有关上载/取消/暂停的article,我来看一下..... 该方法没有帮助
Update(2):我正在尝试提出一种解决方法,如@Greg建议。
Dropbox API的“上传会话”功能是一种通过分段上传大型文件的方法。该应用程序通过首先调用UploadSessionStartAsync
,为每个需要上传的大文件创建一个“上传会话”。每次上传会话的有效期为48小时。
每次对UploadSessionStartAsync
,UploadSessionStartAsync
,UploadSessionStartAsync
的调用都可以包含该文件的一个连续部分。
如果要暂停上载会话,可以通过在这三种方法中的任何一种的调用之间插入所需的任何流控制来实现。只要您再在48小时内使用相同的UploadSessionAppendV2Async
继续运行代码,就可以继续并完成上载会话。