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