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, What are you doing? Batch is used when you want to upload multiple files at once, not to split one file upload and do pieces upload simultaneous!!! Use concurrent upload session to do that.

Rephoto
Helpful | Level 5
Go to solution

Oh sorry,
I will investigate further :((

Rephoto
Helpful | Level 5
Go to solution

@ЗдравкоHi, I tried again with the code below to upload small files (e.g. 10, 20MB). But the result just failed. :))

Rephoto_0-1694572154700.png

 

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

@Rephoto wrote:

@ЗдравкоHi, I tried again with the code below to upload small files (e.g. 10, 20MB). But the result just failed. :))

...


Hm..:thinking_face: Probably I miss something, but where in your code you did upload files actually? 🧐 I cannot figure out where is this place/codeline. Please point out where you pass your files content to. Also don't forget that you should close every single session before finishing - so to be ready for finish! :winking_face:

 

Add: When you have some error, don't forget to post the actual error/unexpected result content too. There is valuable information describing the error.

Rephoto
Helpful | Level 5
Go to solution

@Здравко 
Oh, So I'm missing the file upload part. But I don't know which one to use?

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

@Rephoto wrote:

@Здравко 
Oh, So I'm missing the file upload part. But I don't know which one to use?


In general, you should use UploadSessionAppendV2Async method to upload stream' data. In such case you can combine the batch with big files upload either sequential or concurrent. When you know that only small files are going to upload, using single call of UploadSessionStartAsync to prepare a session (together with data upload and close) may be more suitable (instead of using UploadSessionStartBatchAsync - neither data can be uploaded nor closing it).

Let's hope this clarifies matter.

Rephoto
Helpful | Level 5
Go to solution

@Здравко, Thank :smiling_face_with_heart_eyes:
Can I do it like this? But it gives me an exception ""Error while copying content to a stream." arrival

await _client.Files.UploadSessionFinishAsync(finish, memStream);

Rephoto_0-1694620928952.png

 

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

@Rephoto, Why are you trying to upload the same stream twice? :thinking_face: Previously you skipped upload at all, now just opposite! :slightly_smiling_face:

Something else: Why you skipped finishing the all sessions in batch? In such a way, there is no optimization! Even more - you can do the same with a single call. :winking_face:

Rephoto
Helpful | Level 5
Go to solution

Why are you trying to upload the same stream twice?

Previously you skipped upload at all, now just opposite!
...
@Здравко 

So after I run the UploadSessionStartAsync command, how do I finish uploading to dropbox? :thinking_face: Thanks

 

 

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

If you want to just upload a single small file and that's all, using UploadAsync is the bast. In such a way you create session, upload and close the content and finish the upload - all in this single call. Here is no any optimization. In your last code snippet you make it rather slower - 2 calls instead of one.

If you want to make multiple files upload at once and optimize the process of finish, you can use UploadSessionFinishBatchAsync as you have it done already (or rather tried to use).

That's it. Clarify to yourself what actually you want to do.

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?