我想使用适用于 .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 的引用
我是一名优秀的程序员,十分优秀!