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

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

@Rephoto wrote:

...
I have done uploading small sized files. And I continue to upload files larger than 150MB in size. ...


The only difference when you upload larger files, compared to small files, is that you will need to slice file to pieces and upload every piece to its corresponding place in the file session (not on just some place - as you have tried already for small files). If you will left gap in file... it's error (again), doesn't matter file size!

One more thing you should keep in mind when select files pieces size when concurrent upload is selected - every piece must be uploaded to place which offset is an exact power of 2!!! Violating this rule is error! In general the pieces can be different sized while following previous rule, but an easy way to cover the requirement is to select the same size for all pieces (possibly except the last piece - its size is whatever has been left); a size that is exact power of 2 and not less than 4MB (4*1024*1024).

When some error brings up, post the exact code and point out the exact position.

I will avoid giving language/platform related advises - you should be able debug such errors alone. It's not something Dropbox API related.

Rephoto
Helpful | Level 5
Go to solution

Yes, @Здравко 

Rephoto_0-1694702533420.png

I just got an error when adding this attribute.
And this is the error that I encountered

Rephoto_1-1694702633747.png

 

Greg-DB
Dropbox Staff
Go to solution

@Rephoto It's not entirely clear what that latest issue is, but as Здравко said, it doesn't appear to be from the Dropbox API itself; you'll need to debug this more in your code. Also, it looks like the error message is cut off in your screenshot, so make sure you're reading the whole error message when doing so.

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?