cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Whether you want to work on being more organized, your productivity or you want to use AI to make your life easier, we’ve got something for you right here.

Discuss Dropbox Developer & API

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Error while copying content to a stream in C#

Error while copying content to a stream in C#

Rephoto
Helpful | Level 5
Go to solution

Hi,

I encountered the problem "Error while copying content to a stream" when executing the above code. Is there any way to fix it?

 

try
        {
            var chunkSize = 16 * 1024 * 1024; // 16 MB
            int numChunks = (int)Math.Ceiling((double)fileStream.Length / chunkSize);

            byte[] buffer = new byte[chunkSize];
            string sessionId = null;
            var tasks = new List<Task>();
            for (var idx = 0; idx < numChunks; idx++)
            {
                var byteRead = fileStream.Read(buffer, 0, chunkSize);

                using var memStream = new MemoryStream(buffer, 0, byteRead);
                if (idx == 0)
                {
                    var result = await _client.Files.UploadSessionStartAsync(body: memStream);
                    sessionId = result.SessionId;
                }

                else
                {
                    var cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx));

                    if (idx == numChunks - 1)
                    {
                        await Task.WhenAll(tasks);
                        await _client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(toPathDropbox), body: memStream);
                    }
                    else
                    {
                        tasks.Add(_client.Files.UploadSessionAppendV2Async(cursor, body: memStream));
                    }
                }
            }

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

@Rephoto If you wish to optimize your upload process, please refer to the Performance Guide: https://developers.dropbox.com/dbx-performance-guide

 

That offers some ways you may be able to improve your effective overall upload speed. For example, there are batching and parallelization features you can use.

 

For instance, as Здравко noted, the default upload session type is "sequential", but you can use "concurrent" to upload file pieces in parallel. That would require you to make some more changes to your code to properly send those pieces in parallel though.

View solution in original post

32 Replies 32

Greg-DB
Dropbox Staff
Go to solution

What line is that occurring on? Please print the full exception and stack trace for reference.

Rephoto
Helpful | Level 5
Go to solution

I get an error on this line: "await Task.WhenAll(tasks);"
Is there any way fix it?
Thanks

Rephoto_0-1694531520382.png

 

Rephoto
Helpful | Level 5
Go to solution

Rephoto_0-1694531911221.png

This is another error you may encounter

Greg-DB
Dropbox Staff
Go to solution

Thanks for sharing the additional information. It looks like this is related to you trying to start multiple tasks within the loop without awaiting them, leading to the stream being closed (due to the iteration continuing and moving on to the next piece of the file) before it can be used.

 

Exactly how you structure your code/tasks is up to you, but to get this working I suggest awaiting each append task, instead of using tasks.Add and Task.WhenAll.

Здравко
Legendary | Level 20
Go to solution

Hi @Rephoto,

When you're starting upload session, you use the default type - sequential session, but when actual upload gets on you're trying concurrent upload. Decide what you want actually.

Hope this gives direction.

Rephoto
Helpful | Level 5
Go to solution

I want to increase their upload speed, is there any other way? Thanks

Здравко
Legendary | Level 20
Go to solution

@Rephoto, Start concurrent upload session and upload every peace of data in its own context (so no any reference gets lost). 😉

Good luck.

Greg-DB
Dropbox Staff
Go to solution

@Rephoto If you wish to optimize your upload process, please refer to the Performance Guide: https://developers.dropbox.com/dbx-performance-guide

 

That offers some ways you may be able to improve your effective overall upload speed. For example, there are batching and parallelization features you can use.

 

For instance, as Здравко noted, the default upload session type is "sequential", but you can use "concurrent" to upload file pieces in parallel. That would require you to make some more changes to your code to properly send those pieces in parallel though.

Rephoto
Helpful | Level 5
Go to solution

Rephoto_0-1694539131433.png

I changed the code and there weren't any errors but the Entries returned all had a status of fales.
Can you tell me where I'm going wrong?
Thanks

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    Rephoto Helpful | Level 5
  • User avatar
    Здравко Legendary | Level 20
What do Dropbox user levels mean?