gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:06:34 24 4
gpt4 key购买 nike

我想使用适用于 .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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com