cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Share your feedback on the Document Scanning Experience in the Dropbox App 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);
        }
32 Replies 32

Rephoto
Helpful | Level 5
Go to solution

I have a large number of photos (including about 100 photos under 10Mb). If I use UploadAsync it is getting a lot of too_many_write_operations/ errors.
And that's why I need a new solution to push these photos to Dropbox in a short time. To perform the next tasks

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

Create and upload sessions (and close it) for all files that are less than 150MB in parallel and finally finish all them in a single call. 'too_many_write_operations' is sensitive to number of finishes. When you have many finishes it becomes likely to appears such a error. When you combine them in single call they become less likely.

Rephoto
Helpful | Level 5
Go to solution

So the best way for me to do this task is:
Use UploadSessionStartBatchArg to create sessions, followed by using UploadSessionAppendV2Async to upload each file. And finally, use UploadSessionFinishBatchAsync to complete the task, right?
Thanks

 

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

Hm... Ok right.

Rephoto
Helpful | Level 5
Go to solution

Thank you very much. I'm trying again :beaming_face_with_smiling_eyes: @Здравко 

Rephoto
Helpful | Level 5
Go to solution

Rephoto_0-1694625825795.png

Rephoto_1-1694625852783.png

I have tried this before. But they all got this error :grinning_face_with_sweat:

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

You should debug yourself connection breaks, but there are other... let say strange things in your code. You're starting upload file content at expected file end instead of the beginning. Why? By the way this is error since the file content cannot have "gaps". Even without connection break, you would receive error denoting inconsistent files.

Rephoto
Helpful | Level 5
Go to solution

@Здравко 

Can you point out where my error is? I don't know what to do yet. Thanks

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

изображение.png

Do you understand that on that place has to be the offset in the file in Dropbox session where ongoing upload would go to? In this context, what would be between offset 0 up to memStream.Length? :thinking_face: According your code, there is nothing. :winking_face_with_tongue:

Read the documentation with a bit more care!

Rephoto
Helpful | Level 5
Go to solution

Yes :grinning_face_with_sweat:,
I have done uploading small sized files. And I continue to upload files larger than 150MB in size. But when I use "var result = await _client.Files.UploadSessionStartAsync(body: memStream, sessionType: UploadSessionType.Concurrent.Instance);" I encountered this error ""Error while copying content to a stream.""

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?