cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Are you interested in hearing how one of our Community members uses Dropbox for sailing trips? Read all about it here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

.NET Downloading large file GetContentAsStreamAsync

.NET Downloading large file GetContentAsStreamAsync

JohnnyNPC
Explorer | Level 3
Go to solution

I want to use the Dropbox. Api for .NET to allow my users to download files I have stores in Dropbox from my site.

The size of the files ranges from small (a few Mb) to Big (a couple of Hundred Mb)

This is the code I have:

public async void DownloadWithTracking(string argFilePath)
    {
        var aFileName = Path.GetFileName(argFilePath);
        
        using (var aDropboxClient = new DropboxClient(anAccessToken))
        {
            var aResponse = await aDropboxClient.Files.DownloadAsync(argFilePath);

            ulong aFileSize = aResponse.Response.Size;
            const int aBufferSize = 4 * 1024 * 1024;

            var aBuffer = new byte[aBufferSize];

            using (var aDropboxContentStream = await aResponse.GetContentAsStreamAsync())
            {
                Response.BufferOutput = false;
                Response.AddHeader("content-disposition", "attachment;filename=" + aFileName);
                Response.AddHeader("Content-Length", aFileSize.ToString());
                int aLengthOfBytesRead = aDropboxContentStream.Read(aBuffer, 0, aBufferSize);
                while (aLengthOfBytesRead > 0)
                {
                    Response.OutputStream.Write(aBuffer, 0, aLengthOfBytesRead);
                    aLengthOfBytesRead = aDropboxContentStream.Read(aBuffer, 0, aBufferSize);
                }
            }
        }
    }

 

If the files are small this code works perfectly but for the bigger files I get an error with this stackTrace:

 

System.IO.IOException was unhandled by user code
  HResult=-2146232800
  Message=The read operation failed, see inner exception.
  Source=System.Net.Http
  StackTrace:
       at System.Net.Http.HttpClientHandler.WebExceptionWrapperStream.Read(Byte[] buffer, Int32 offset, Int32 count)
       at System.Net.Http.DelegatingStream.Read(Byte[] buffer, Int32 offset, Int32 count)
       at test_DropboxTest.<DownloadWithTracking>d__0.MoveNext() in d:\Dropbox\NPC Website\website\test\DropboxTest.aspx.cs:line 41
  InnerException: System.Net.WebException
       HResult=-2146233079
       Message=The request was aborted: The request was canceled.
       Source=System
       StackTrace:
            at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
            at System.Net.Http.HttpClientHandler.WebExceptionWrapperStream.Read(Byte[] buffer, Int32 offset, Int32 count)
       InnerException: 

This happens after a few 10's of MB (from 20-50 Mb) 

 

Thanks in advance

 

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution
5 Replies 5

Greg-DB
Dropbox Staff
Go to solution

[Cross-linking for reference: https://stackoverflow.com/questions/43208069/dropbox-net-downloading-large-files-using-getcontentass... ]

 

I can't seem to reproduce this issue. Is it failing after a certain amount of time? If so, it may be timing out, which would vary by network connection.

 

Alternatively, are you trying to download multiple files at once? If so, how many?

JohnnyNPC
Explorer | Level 3
Go to solution

It's failing after some time... I am downloading a large file, in the case of the test it's a file that's 245 MB.

Downloading this file directly from dropbox took about 6 minutes.

Is there a way to set the time out  for a longer period?

 

Greg-DB
Dropbox Staff
Go to solution
How long does it take to fail when you're trying to download via the API? If it is a timeout issue, there may be a way to configure that, but we'll have to look into it.

Also, are you trying to download multiple files at once?

JohnnyNPC
Explorer | Level 3
Go to solution
It takes one and a half to two minutes, give or take.
In the internal tests I'm running now I'm only trying to download one file at the time.

Greg-DB
Dropbox Staff
Go to solution
Thanks! In that case, you can specify a longer timeout as shown here:

https://github.com/dropbox/dropbox-sdk-dotnet/blob/master/Examples/SimpleTest/Program.cs#L49
Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    JohnnyNPC Explorer | Level 3
What do Dropbox user levels mean?