You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

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

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.

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

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

    • Rephoto's avatar
      Rephoto
      Helpful | Level 5

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

       

    • Rephoto's avatar
      Rephoto
      Helpful | Level 5

      This is another error you may encounter

      • Greg-DB's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox Staff

        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.