One month down in 2025: How are your resolutions coming along? Check out how to get back on track here.
Forum Discussion
roman prog 89 level_
3 years agoExplorer | Level 3
How to continue if the connection is interrupted.
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);
}
}
}
}
}
}
}
The "Access to a closed stream is not possible" error message doesn't appear to be coming from the Dropbox API itself, so I can't offer too much help with that in particular. That said, it's likely referring to that "memStream" object. It looks like that's closed, since it was used in the first attempt, so you'd need to make a new one for the new attempt.
- Greg-DB
Dropbox Staff
I see you're using upload sessions to upload files by doing so in multiple pieces. With this functionality, you can catch and handle any exceptions that may occur, and then resume the process by picking up with whichever upload session call you last made.
For instance, if a particular call, such as UploadSessionStartAsync or UploadSessionAppendV2Async, for instance, fails due to a network issue, you can have your code catch the exception and then retry that particular call.
Note that in some cases, depending on the exact nature and timing of the disconnection, it's possible for the Dropbox servers to receive the complete call, without your client receiving the response for that call. In that kind of case, the client and server may then disagree on the current offset. If that happens, on the next call for the upload session, the client will get a UploadSessionLookupError.IncorrectOffset with the UploadSessionOffsetError containing the CorrectOffset that it can use to resume the upload session at the correct offset.
- roman prog 89 level_Explorer | Level 3
yes, I am doing error trapping.
But when I try to re-upload it doesn't work. although the connection has already been restored. I'm trying it on the bench.
It is noticeable that he does not even try to unload.
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); //there is no reloading at this point.
}
catch (Exception er)
{
Log("TRAYING ERROR - " + " - " + er.Message);
}
}
}- Greg-DB
Dropbox Staff
Can you elaborate on what you mean when you say "it doesn't work"? What unexpected error, output, or behavior are you getting? I recommend stepping through with the debugger first to see what is being executed and when to understand what is happening.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,950 PostsLatest Activity: 2 years ago
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 or Facebook.
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!