cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
We want to hear from you. What do you use Dropbox for?
Close

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Highlighted

Dropbox API Error on Upload: Incorrect offset

Explorer | Level 3
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
4 Replies 4
Highlighted

Re: Dropbox API Error on Upload: Incorrect offset

Explorer | Level 3

Hi, could you solve the problem?

 

Highlighted

Re: Dropbox API Error on Upload: Incorrect offset

Dropboxer
Dropboxer

It seems I missed this thread back in May (apologies!), but in general an 'incorrect_offset' error happens when the app calls one of the append or finish upload session endpoints (or corresponding native methods in an SDK) with an "offset" value that does not match how much has actually been uploaded so far for that upload session.

For example, this can happen due to a bug in the app's code, or if there is a network issue that stops the API response from reaching the app, so it doesn't know if that piece of the upload session was successful.

In any case, the app should read the returned 'correct_offset' value and resume uploading from that point in the file.

Highlighted

Re: Dropbox API Error on Upload: Incorrect offset

Explorer | Level 3
 

Thank you,
Just asking, because I could solve it with the code here.

Highlighted

Re: Dropbox API Error on Upload: Incorrect offset

Explorer | Level 3

Private Async Function ChunkUpload(Ipath As String, stream As IO.FileStream, chunkSize As Integer) As Task
Dim numChunks As Integer = Int(Math.Ceiling(stream.Length / chunkSize))
Dim buffer() As Byte = New Byte(chunkSize) {}
Dim II As String = DBLocation.Remove(0, DBLocation.LastIndexOf("\") + 1)
''ant
''Dim sessionId As String = String.Empty
''new
Dim sessionId As String = Nothing
Cursor = Cursors.WaitCursor

''For idx As Integer = 0 To chunkSize - 1
''new
For idx As Integer = 0 To numChunks - 1 Step 1
''idx += 1
Dim byteRead = stream.Read(buffer, 0, chunkSize)
''Dim memstream = New IO.MemoryStream(buffer, 0, byteRead)
''new
Dim memstream As MemoryStream = New MemoryStream(buffer, 0, byteRead)
If idx = 0 Then
''Dim result = Await DBX.Files.UploadSessionStartAsync(False, memstream)
''new
Dim result = Await DBX.Files.UploadSessionStartAsync(body:=memstream)
sessionId = result.SessionId
Else
Dim cursor As UploadSessionCursor = New UploadSessionCursor(sessionId, CLng(chunkSize * idx))
If idx = numChunks - 1 Then
''Await DBX.Files.UploadSessionFinishAsync(cursor, New CommitInfo(Ipath), memstream)
Await DBX.Files.UploadSessionFinishAsync(cursor, New CommitInfo(Ipath + "/" + II), memstream)

Else
''Await DBX.Files.UploadSessionAppendV2Async(cursor, False, memstream)
''new
Await DBX.Files.UploadSessionAppendV2Async(cursor, False, body:=memstream)

End If
End If
Next
End Function

Polls
Do you know how to organize your files and folders?
<b>We have created a guide on folder best practice, so you can get organized now <a href="https://www.dropboxforum.com/t5/Business-Tips-tricks/Folder-structure-best-practice/m-p/428456#M39"><u>check it out here</u></a>.</b>

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Who's talking

Top contributors to this post

What do Dropbox user levels mean?
Need more support?