Your workflow is unique 👨‍💻 -  tell us how you use Dropbox here.

Forum Discussion

Jonathan V.5's avatar
Jonathan V.5
New member | Level 1
10 years ago

API v2 .NET, problem with upload session

Here is my code. If the file is small, the UploadAsync function is called and works flawlessly.

However, for larger file, the chunk upload fails. I get a JSON error the first time UploadSessionAppendAsync is called.

Any idea  what could be wrong ?


private async Task Upload(string file, string targetPath)
{
const int chunkSize = 1024*1024;
CurrentFileStream = File.Open(file, FileMode.Open, FileAccess.Read);

if(CurrentFileStream.Length <= chunkSize)
{
System.Diagnostics.Trace.WriteLine("Start one-shot upload");
await Client.Files.UploadAsync(targetPath, body: CurrentFileStream);
}

else
{
System.Diagnostics.Trace.WriteLine("Start chunk upload");
await ChunkUpload(targetPath, chunkSize);
}
}

private async Task ChunkUpload(String path, int chunkSize)
{
int numChunks = (int)Math.Ceiling((double)CurrentFileStream.Length / chunkSize);

byte[] buffer = new byte[chunkSize];
string sessionId = null;

for(var idx = 0; idx < numChunks; idx++)
{
var byteRead = CurrentFileStream.Read(buffer, 0, chunkSize);
System.Diagnostics.Trace.WriteLine(byteRead.ToString());

using(MemoryStream memStream = new MemoryStream(buffer, 0, byteRead))
{
if(idx == 0)
{
System.Diagnostics.Trace.WriteLine("Session start");
var result = await Client.Files.UploadSessionStartAsync(memStream);
sessionId = result.SessionId;
}

else
{
System.Diagnostics.Trace.WriteLine("Upload cusor");
UploadSessionCursor cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx));

if(idx == numChunks - 1)
{
System.Diagnostics.Trace.WriteLine("Session finish");
await Client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(path), memStream);
}

else
{
System.Diagnostics.Trace.WriteLine("Session append");
await Client.Files.UploadSessionAppendAsync(cursor, memStream);
}
}
}
}
}



************** Exception Text **************
System.InvalidOperationException: Invalid json token. Expect start object
at Dropbox.Api.Babel.StructDecoder`1.EnsureStartObject(IJsonReader reader)
at Dropbox.Api.Babel.StructDecoder`1.Decode(IJsonReader reader)
at Dropbox.Api.Babel.JsonReader.Read[T](String json, IDecoder`1 decoder)
at Dropbox.Api.DropboxRequestHandler.<Dropbox.Api.Babel.ITransport.SendUploadRequestAsync>d__5`3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Archivist.MainForm.<ChunkUpload>d__25.MoveNext() in c:\Dropbox\Archivist\Archivist\Form1.cs:line 290
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Archivist.MainForm.<Upload>d__21.MoveNext() in c:\Dropbox\Archivist\Archivist\Form1.cs:line 251
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Archivist.MainForm.<UploadHighlightedButton_Click>d__1a.MoveNext() in c:\Dropbox\Archivist\Archivist\Form1.cs:line 233
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__4(Object state)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
Archivist
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Dropbox/Archivist/Archivist/bin/Release/Archivist.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.34238 built by: FX452RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
Accessibility
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------
System.Configuration
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.34234 built by: FX452RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
Dropbox.Api
Assembly Version: 0.1.5801.29559
Win32 Version: 0.1.0.0
CodeBase: file:///C:/Dropbox/Archivist/Archivist/bin/Release/Dropbox.Api.DLL
----------------------------------------
System.Net.Http.WebRequest
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Net.Http.WebRequest/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Net.Http.WebRequest.dll
----------------------------------------
System.Net.Http
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Net.Http/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Net.Http.dll
----------------------------------------
System.Core
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Threading.Tasks
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.34209
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Threading.Tasks/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Threading.Tasks.dll
----------------------------------------
Microsoft.Threading.Tasks
Assembly Version: 1.0.12.0
Win32 Version: 1.0.168.0
CodeBase: file:///C:/Dropbox/Archivist/Archivist/bin/Release/Microsoft.Threading.Tasks.DLL
----------------------------------------
Newtonsoft.Json
Assembly Version: 7.0.0.0
Win32 Version: 7.0.1.18622
CodeBase: file:///C:/Dropbox/Archivist/Archivist/bin/Release/Newtonsoft.Json.DLL
----------------------------------------
Microsoft.Threading.Tasks.Extensions
Assembly Version: 1.0.12.0
Win32 Version: 1.0.168.0
CodeBase: file:///C:/Dropbox/Archivist/Archivist/bin/Release/Microsoft.Threading.Tasks.Extensions.DLL
----------------------------------------
System.Net
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.34209
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Net/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Net.dll
----------------------------------------

13 Replies

Replies have been turned off for this discussion
  • Priya R.3's avatar
    Priya R.3
    New member | Level 1
    9 years ago

    I have started to get upload error in my windows service which uses dropbox api. It was running fine from last one month and first time I have encountered the issue. I have limited error text captured via service which i am posting below.

    Any hint why it is happening. File size is 6 MBand it is a pdf file.

     

    ----------------------ERROR TEXT---------------------------

    ---------------- Message ---------------
    A task was canceled.
    --------- Stack Trace ---------
    at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
    at Dropbox.Api.DropboxRequestHandler.<Dropbox.Api.Babel.ITransport.SendUploadRequestAsync>d__5`3.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
    at DropBoxService.DropBoxLocation.VB$StateMachine_0_Upload.MoveNext()
    ------------ Target Site ---------
    Void ThrowForNonSuccess(System.Threading.Tasks.Task)
    ----------------System.Threading.Tasks.TaskCanceledException: A task was canceled.
    at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
    at Dropbox.Api.DropboxRequestHandler.<Dropbox.Api.Babel.ITransport.SendUploadRequestAsync>d__5`3.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
    at DropBoxService.DropBoxLocation.VB$StateMachine_0_Upload.MoveNext()

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!