c# - Dropbox .NET 使用 GetContentAsStreamAsync 下载大文件

标签 c# .net dropbox-api

我想使用适用于 .NET 的 Dropbox Api 来允许我的用户从我的站点下载我存储在 Dropbox 中的文件。换句话说,我想将大文件保存在 Dropbox 并使用 Dropbox 作为存储。 文件的大小范围从小(几 MB)到大(几百 MB) 这是我的代码:

public async void DownloadChunks(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);
            }
        }
    }
}

此代码基于 Dropbox 在其文档中用于跟踪下载进度的代码。 当文件很小时,我可以很好地下载它们。我什至可以使用更简单的代码一次性下载,而不是按 4MB 的 block 下载。但是当文件更大时,我可以使用数据 block 下载更多内容。 但是对于更大的文件(大于 20MB),我仍然遇到这个错误,指出请求被中止。 这是 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: 

提前致谢

最佳答案

问题实际上是我必须在 Dropbox 客户端中设置的超时,正如 Greg 在 Dropbox 论坛中指出的那样。 我将最终代码放在这里可能会在将来帮助某人...

public async void DownloadWithTracking(string argFilePath)
    {
        var aFileName = Path.GetFileName(argFilePath);
        string anAccessToken = "";

        var aHttpClient = new HttpClient(new WebRequestHandler { ReadWriteTimeout = 10 * 1000 }) { Timeout = TimeSpan.FromMinutes(20) };
        var aDorpboxConfig = new DropboxClientConfig("DownloadApp") { HttpClient = aHttpClient };

        using (var aDropboxClient = new DropboxClient(anAccessToken, aDorpboxConfig))
        {
            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);
                }
            }
        }
    }

请注意,我必须添加对 System.Net.Http.WebRequest 的引用

关于c# - Dropbox .NET 使用 GetContentAsStreamAsync 下载大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43208069/

相关文章:

viewbox 中的 C# wpf 控件,但无法获得实际大小

c# - C# 中的 `??` 线程安全吗?

c# - List<> 在不同的类中有不同的行为

javascript - 如何使用 Dropbox Drop-in 应用程序始终从我的帐户中选择文件?

javascript - 在 JQuery 而不是 Javascript 中读取所选的 Dropbox 响应

c# - 将具有通用和具体实现的存储库注入(inject) Controller

c# - 为什么仅在实现接口(interface)后才重写方法?

c# - 如何通过鼠标拖动来选择多个控件

.net - FileIO.FileSystem.CopyFile() 与 System.IO.File.Copy()

javascript - 将图像与 Canvas 中的数据保存到保管箱