Hi everyone.
File unload question.
How to continue if the connection is interrupted.
An example of my code.
using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
int numChunks = (int)Math.Ceiling((double)stream.Length / chunkSizeByte);
byte[] buffer = new byte[chunkSizeByte];
string sessionId = null;
for (var idx = 0; idx < numChunks; idx++)
{
var byteRead = stream.Read(buffer, 0, chunkSizeByte);
using (MemoryStream memStream = new MemoryStream(buffer, 0, byteRead))
{
if (idx == 0)
{
try
{
var result = await dbx2.Files.UploadSessionStartAsync(close:false , body: memStream);
sessionId = result.SessionId;
Log(DateTime.Now + " - TRYING - " + i + " - FRAGMENT FIRST - " + idx);
}
catch (Exception ui)
{
Log(DateTime.Now + " - TRYING - " + i + " - FRAGMENT FIRST ERROR - " + ui.Message);
}
}
else
{
ulong size = chunkSize * (ulong)idx;
UploadSessionCursor cursor = new UploadSessionCursor(sessionId, size);
if (idx == numChunks - 1)
{
try
{
await dbx2.Files.UploadSessionFinishAsync(cursor, new CommitInfo(folder + "/" + Date + filenamepath), memStream);
Log(DateTime.Now + " - TRYING - " + i + " - FRAGMENT LAST - " + idx);
}
catch (Exception gh)
{
Log(DateTime.Now + " - TRYING - " + i + " - FRAGMENT LAST ERROR - " + gh.Message);
}
}
else
{
try
{
await dbx2.Files.UploadSessionAppendV2Async(cursor, close:false, body: memStream); // if there is an upload error
Log(DateTime.Now + " - TRYING - " + i + " - FRAGMENT PART - " + idx);
}
catch (Exception yt)
{
Log(yt.Message + " - - - " + yt.InnerException + " - - - " + yt.TargetSite);
System.Threading.Thread.Sleep(pause);
try
{
await dbx2.Files.UploadSessionAppendV2Async(cursor, close: false, body: memStream);
}
catch (Exception er)
{
Log("TRAYING ERROR - " + " - " + er.Message);
}
}
}
}
}
}
}