cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Back again with another edition of 'How we use Dropbox', so find out how Emma uses to-do lists to get it all done 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: 

chunk upload error if only one chunk c#

chunk upload error if only one chunk c#

Julien_Do
Explorer | Level 3

Hi,

I'm using the chunk upload and it works fine. I want to use it for all the files not just for the big ones because it is easy to trace my upload. I'm facing un error when the file is smaller than the chunk:

InnerException = {"Cannot access a closed Stream."}

Message = "Error while copying content to a stream."

 

my code is: 

using (var stream = new MemoryStream(File.ReadAllBytes(path)))
{
int numChunks = (int)Math.Ceiling((double)stream.Length / chunkSize);

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

for (var idx = 0; idx < numChunks; idx++)
{
var byteRead = stream.Read(buffer, 0, chunkSize);

using (MemoryStream memStream = new MemoryStream(buffer, 0, byteRead))
{
if (idx == 0)
{
var result = await client.Files.UploadSessionStartAsync(body: memStream);
sessionId = result.SessionId;
if (idx == numChunks - 1)
{
UploadSessionCursor cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx));
await client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(folder + "/" + fileName), memStream);
}
}
else
{
UploadSessionCursor cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx));
if (idx == numChunks - 1)
{
await client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(folder + "/" + fileName), memStream);
}
else
{
await client.Files.UploadSessionAppendV2Async(cursor, body: memStream);
}
}
}
}
}

Is there any way to upload a a chunk size file with chunk upload or the only way is to use the normal upload for the files smaller than the chunk?

Thanks in advance!

1 Accepted Solution

Accepted Solutions

Re: chunk upload error if only one chunk c#

Greg-DB
Dropboxer

You can use upload sessions (a.k.a. "chunk upload") to upload small files. Using the normal upload is more efficient for small files though, since you only need to make one call as opposed to two, so I would recommend using that, despite the bit of added code complexity.

If you do want to use upload sessions to upload small files, you would need two calls:

  1. `UploadSessionStartAsync`: to start the upload session and upload the data.
  2. `UploadSessionFinishAsync`: to finish the upload session and commit the file. Make sure you send the correct offset.

(You can technically send the file data on either of the calls actually.)

For the error you are getting though, I'd first check what line it's occurring on. It looks like you're attempting to read from a stream, presumably one of the two `MemoryStream`s you open, when the stream is already closed. I recommend stepping through with the debugger to track down the issue.

 

View solution in original post

2 Replies 2

Re: chunk upload error if only one chunk c#

Greg-DB
Dropboxer

You can use upload sessions (a.k.a. "chunk upload") to upload small files. Using the normal upload is more efficient for small files though, since you only need to make one call as opposed to two, so I would recommend using that, despite the bit of added code complexity.

If you do want to use upload sessions to upload small files, you would need two calls:

  1. `UploadSessionStartAsync`: to start the upload session and upload the data.
  2. `UploadSessionFinishAsync`: to finish the upload session and commit the file. Make sure you send the correct offset.

(You can technically send the file data on either of the calls actually.)

For the error you are getting though, I'd first check what line it's occurring on. It looks like you're attempting to read from a stream, presumably one of the two `MemoryStream`s you open, when the stream is already closed. I recommend stepping through with the debugger to track down the issue.

 

View solution in original post

Re: chunk upload error if only one chunk c#

Julien_Do
Explorer | Level 3

Yes, it was a  problem with the offset.

Thank you!

Poll
We love to learn from the educators who use Dropbox. Whether you teach kids, teens, adults or a combination of all three, we want to know what apps and integrations you use with Dropbox to help with teaching. Which of the ones below is your favorite, or most used tool?
Who's talking

Top contributors to this post

  • User avatar
    Julien_Do Explorer | Level 3
  • User avatar
    Greg-DB Dropboxer
What do Dropbox user levels mean?
Need more support?