We are aware of the issue with the badge emails resending to everyone, we apologise for the inconvenience - learn more here.

Forum Discussion

JohnnyNPC's avatar
JohnnyNPC
Explorer | Level 3
8 years ago

.NET Downloading large file GetContentAsStreamAsync

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

 

    • JohnnyNPC's avatar
      JohnnyNPC
      Explorer | Level 3

      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's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox Staff
        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?

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,877 PostsLatest Activity: 12 months ago
325 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!