0

Im trying to make some upload folder function using dropbox api within my vb.net application. i already got the code but it just for the file. can you guys help to solve this problem ?, what function i shoul use ?. this my code

 Public Async Sub UploadToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles UploadToolStripMenuItem1.Click
    Using FolderBrowserDialog As New FolderBrowserDialog
        If FolderBrowserDialog.ShowDialog() <> Windows.Forms.DialogResult.OK Then Return
        Dim alamat As String = FolderBrowserDialog.SelectedPath
        Dim nama As String = New DirectoryInfo(alamat).Name
        Dim uploadname As String = "\" & nama
        Await Uploadfolder(alamat, uploadname)
        'Dim uploadfolder = A.Files.UploadSessionAppendV2Async
        'Dim FileCount As Integer = 0
        'My.Computer.FileSystem.CopyDirectory(alamat, folder & "/" + nama, True)


    End Using
End Sub

Private Async Function Uploadfolder(localPath As String, remotePath As String) As Task
    Const ChunkSize As Integer = 4096 * 1024
    Using Directory.EnumerateFiles = 
    End Using
    Using FileStream = File.Open(localPath, FileMode.Append)
        If FileStream.Length <= ChunkSize Then
            Await A.Files.UploadAsync(remotePath, body:=FileStream)
        Else
            Await ChunkUpload(remotePath, FileStream, ChunkSize)
        End If
    End Using
End Function

Private Async Function ChunkUpload(path As [String], stream As FileStream, chunkSize As Integer) As Task
    Dim numChunks As Integer = CInt(Math.Ceiling(CDbl(stream.Length) / chunkSize))
    Dim buffer As Byte() = New Byte(chunkSize - 1) {}
    Dim sessionId As String = Nothing
    For idx = 0 To numChunks - 1
        Dim byteRead = stream.Read(buffer, 0, chunkSize)

        Using memStream = New MemoryStream(buffer, 0, byteRead)
            If idx = 0 Then
                Dim result = Await A.Files.UploadSessionStartAsync(False, memStream)
                sessionId = result.SessionId
            Else
                Dim cursor = New UploadSessionCursor(sessionId, CULng(CUInt(chunkSize) * CUInt(idx)))

                If idx = numChunks - 1 Then
                    Dim fileMetadata As FileMetadata = Await A.Files.UploadSessionFinishAsync(cursor, New CommitInfo(path), memStream)
                    Console.WriteLine(fileMetadata.PathDisplay)
                    MsgBox("Successfully Uploaded")
                Else
                    Await A.Files.UploadSessionAppendV2Async(cursor, False, memStream)
                End If
            End If
        End Using
    Next
End Function
  • The Dropbox API upload sessions functionality only allows for uploading files, not entire folders at once. You'll need to loop through the files in the folder you want to upload and upload each one. – Greg Jan 17 '17 at 00:07
  • oh, okay greg, so i must loop create folder and upload file within ? – Andru Deva Lukito Jan 17 '17 at 04:03
  • You actually don't need to make calls to create the folders. When you upload a file, the parent folders in the destination path will automatically be created if they don't already exist. You do need to loop through all of the files you want to upload and upload each one though. – Greg Jan 17 '17 at 17:50
  • oh okay greg, thanks for the advice, i want to upload a folder that have many subfolder and also files, without breaking down to the last file inside the folder, so i already made the function that we can do it just only select the top folder or we can call it root – Andru Deva Lukito Jan 18 '17 at 01:09

0 Answers0