Reputation: 194
I use VB.Net with .NET Framework 4.6.2 on Windows 10 32bit, my file is almost 12mb.
I use this code to upload files to 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
I already have another function returns Access-Token and stores it in My.Settings.AccessToken
. In my Form, I call this function to upload the File :
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
My question is : How to Pause/Resume/Stop uploading the file ?
Update (1) : I have found this article about Upload/Cancel/Pause, I will take a look..... That method was not helpful
Update (2) : I'm trying to come up with a work around method as @Greg suggested.
Upvotes: 1
Views: 631
Reputation: 16940
The Dropbox API "upload session" functionality is a way to upload large files by doing so in pieces. The app creates one "upload session" per large file that it needs to upload, by initially calling UploadSessionStartAsync
. Each upload session is valid for 48 hours.
Each call to UploadSessionStartAsync
, UploadSessionAppendV2Async
, UploadSessionFinishAsync
can contain one consecutive piece of the file.
If you wish to pause the upload session, you can do so by inserting whatever flow control you want between the calls to any of those three methods. As long as you then continue running the code using the same cursor
, within 48 hours, you can then continue and finish the upload session.
Upvotes: 2