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 ****** (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