cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Share your feedback on the Document Scanning Experience in the Dropbox App right here.

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: 

API v2 .NET, problem with upload session

API v2 .NET, problem with upload session

Jonathan V.5
New member | Level 1

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 13

Greg-DB
Dropbox Staff

This should be fixed now in version 2.0.3 of the SDK, just released. Please give that a try and let me know if you're still having trouble. 

Jonathan V.5
New member | Level 1

Upload works now, thanks for the quick fixes.

Priya R.3
New member | Level 1

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()

Greg-DB
Dropbox Staff

Hi Priya, please start a new thread with the steps to reproduce the issue, the relevant code snippet(s), and the version number of the SDK you're using, so that we can look into it without spamming this old thread:

https://www.dropboxforum.com/hc/en-us/community/posts/new?community_post%5Btopic_id%5D=200209245

Thanks in advance! 

 

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    Priya R.3 New member | Level 1
  • User avatar
    Jonathan V.5 New member | Level 1
What do Dropbox user levels mean?