We Want to Hear From You! What Do You Want to See on the Community? Tell us here!

Forum Discussion

Rephoto's avatar
Rephoto
Helpful | Level 5
2 years ago
Solved

Error while copying content to a stream in C#

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);
        }
  • 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.

32 Replies

Replies have been turned off for this discussion

About Discuss Dropbox Developer & API

Node avatar for Discuss Dropbox Developer & API
Make connections with other developers815 PostsLatest Activity: 5 hours ago
276 Following

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

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!