使用uploadsessionstart通过dropbox api上传文件夹(vb.net)

时间:2017-01-16 06:18:43

标签: vb.net upload directory dropbox dropbox-api

我试图在我的vb.net应用程序中使用dropbox api创建一些上传文件夹功能。我已经得到了代码,但它只是为了文件。你能帮我解决这个问题吗?我应该用什么功能?这是我的代码

 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

0 个答案:

没有答案