abbrechen
Suchergebnisse werden angezeigt für 
Stattdessen suchen nach 
Meintest du: 

default

no

Highlighted

Dropbox API Error on Upload: Incorrect offset

hello, I have written an app with which you can upload all kinds of files into a dropbox folder. I have already spent several hours trying to fix the error on my own, but I can not find a solution. Whenever I try to upload a file larger than about 30MB, the error message "incorrect_offset" appears.
Imports System.IO
Imports System.Net
Imports Dropbox.Api
Imports Dropbox.Api.Files

Public Class Form1

    'Global variable for the Access-Token
    Dim client As DropboxClient = New DropboxClient("YOUR TOKEN")
    'Global variable to set the Dropbox Folderpath
    Dim remotepath As String = "/UploadVideoToDB"
    'This is the path for your desired file
    Dim path As String = ""
    'This variable saves only the file name 
    Dim filename As String = ""

    'This function is not called until the button 'btn_choose_file_click' is clicked
    Private Sub btn_choose_file_click(sender As Object, e As EventArgs) Handles btn_choose_file.Click

        'This part is there to select a file with the help of the openfiledialog

        'This is the start folder that will be displayed when the openfiledialog opens
        OpenFileDialog1.InitialDirectory = "C:\"

        'Setting up the Title of the openfiledialog window
        OpenFileDialog1.FileName = "Open A File..."

        'That means you can only select one file (please dont change to 'true')
        OpenFileDialog1.Multiselect = False

        'This defines which files may be selected
        OpenFileDialog1.Filter = "All Files|*.*"

        'A little if-query that checks if the openfiledialog returns 'ok' as result. 
        'If so, then the text Of lbl_filename Is changed To the name Of the selected file. 
        'In addition, the variable 'path' is assigned the path of the selected file as the value and the variable 'filename' the name.
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            lbl_filename.Text = OpenFileDialog1.FileName
            path = OpenFileDialog1.FileName
            filename = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
        End If
    End Sub

    'This function is not called until the button 'btn_upload_click' is clicked
    Private Sub btn_upload_Click(sender As Object, e As EventArgs) Handles btn_upload.Click

        'These lines deactivate the two buttons (so that they can not be clicked repeatedly) 
        'And call the function 'Upload' with the parameters 'remotepath & filename'
        btn_choose_file.Enabled = False
        btn_upload.Enabled = False

        Upload(remotepath & "/" & filename)
    End Sub


    'remotepath = remotepath & "/" & filename
    Private Async Sub Upload(remotePath As String)

        'This line determines how big the individual packages sent should be. this value may not exceed 150MB. (PLease dont change this value)
        Const ChunkSize As Integer = 8024 * 1024

        'This filestream reads the file under the given path in 'path'
        Using localfile As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read)

            'Checks if the selected file is bigger then the determined ChunkSize
            If localfile.Length <= ChunkSize Then
                Try
                    'Gives the cursor a little loading animation so the user can see that something happens
                    Cursor = Cursors.WaitCursor

                    'Uploads the selected file to the Dropbox folder
                    Await client.Files.UploadAsync(remotePath, body:=localfile)

                    'Resets the cursor to default
                    Cursor = Cursors.Default


                    MessageBox.Show("Successfully Uploaded!")

                    'Enables both buttons
                    btn_choose_file.Enabled = True
                    btn_upload.Enabled = True

                    'If an error has occurred, it will be displayed in a messagebox
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                    btn_choose_file.Enabled = True
                    btn_upload.Enabled = True
                    Cursor = Cursors.Default
                End Try
            Else
                Try
                    'if the file is larger than the variable 'ChunkSize' the function 'ChunkUpload' is called
                    Await ChunkUpload(remotePath, filename, ChunkSize)

                    'If an error has occurred, it will be displayed in a messagebox
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                    btn_choose_file.Enabled = True
                    btn_upload.Enabled = True
                    Cursor = Cursors.Default
                End Try
            End If
        End Using
    End Sub

    'folder = remotepath
    'fileName = filename
    'chunksize = ChunkSize
    Private Async Function ChunkUpload(folder As String, fileName As String, chunksize As Integer) As Task


        'This filestream reads the file under the given path in 'path'
        Dim stream = New FileStream(path, FileMode.Open, FileAccess.Read)

        'This function splits the size of the selected file into equal chuck packages and returns the value as an integer
        Dim numChunks As Integer = CInt(Math.Ceiling(CDbl(stream.Length) / chunksize))

        'The variable 'chunksize' is stored as byte
        Dim buffer As Byte() = New Byte(chunksize) {}

        Dim sessionId As String = Nothing

        Cursor = Cursors.WaitCursor

        'This for loop runs until all packages have been transferred
        For idx = 0 To numChunks Step 1


            Dim byteRead = stream.Read(buffer, 0, chunksize)

            Using memStream As MemoryStream = New MemoryStream(buffer, 0, byteRead)

                If idx = 0 Then

                    'If the counter 'idx' has the value 0, then the upload is started
                    Dim result = Await client.Files.UploadSessionStartAsync(body:=memStream)

                    'The variable 'sessionid' stores the ID for this upload process. Each upload process has its individual ID
                    sessionId = result.SessionId
                Else

                    'Unfortunately, I can not write anything about this line. I'm not sure what it is there for. I am sorry!
                    Dim cursor As UploadSessionCursor = New UploadSessionCursor(sessionId, chunksize)


                    If idx = numChunks - 1 Then

                        'when 'idx' reaches the value of 'numchunks', the upload process is completed. The file is now created in the DropBox account and all packages are put together.
                        Await client.Files.UploadSessionFinishAsync(cursor, New CommitInfo(folder & "/" & fileName), memStream)
                    Else

                        'More packages will be uploaded
                        Await client.Files.UploadSessionAppendV2Async(cursor, False, body:=memStream)
                    End If
                End If
            End Using
        Next

        'Resets the cursor to default
        Cursor = Cursors.Default

        MessageBox.Show("Successfully uploaded!")

        'Enables both buttons
        btn_choose_file.Enabled = True
        btn_upload.Enabled = True
    End Function

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'When starting the app, 'Form1' will be displayed in the center of the screen
        Me.CenterToScreen()

    End Sub
End Class
Dropbox API Error on Upload: Incorrect offset
17 Aufrufe
0 Antworten
0 Likes
Wer spricht
Hauptteilnehmer bei diesem Post