cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: Need .Net sample for download to file

Need .Net sample for download to file

sanjayssk
Helpful | Level 6
Go to solution

I'm using Dropbox API in an asp.net application. I have managed to download the file contents into a stream upto the following line of code in an async task.

 

var strm = response.GetContentAsStreamAsync();

 

But I can't find a sample of how to write this stream finally to a file on the web server for which I do have a path DestPath. I have tried using some .Net code to write Stream to file but that complains and can not compile because the above strm and IO.Stream are different. I can't find a sample in .Net SDK for dropbox. Can someone help?

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

The GetContentAsStream method yields a Stream, so you should be able to use CopyTo like this:

 

var strm = await response.GetContentAsStreamAsync();

using (var fileStream = File.Create(localFilePath))
{
    strm.CopyTo(fileStream);
}

View solution in original post

8 Replies 8

Greg-DB
Dropbox Staff
Go to solution

The GetContentAsStream method yields a Stream, so you should be able to use CopyTo like this:

 

var strm = await response.GetContentAsStreamAsync();

using (var fileStream = File.Create(localFilePath))
{
    strm.CopyTo(fileStream);
}

sanjayssk
Helpful | Level 6
Go to solution

 

Thanks for the speedy reply! I was missing the "await" and hence it was complaining that CopyTo is not a method of Task<Stream>.

 

 

sanjayssk
Helpful | Level 6
Go to solution

Any ideas how to catch a Timeout exception for large downloads? I tried using Try/Catch but it is not raised.

 

Update: I tried this code but it doesn't cause a timeout.

 

var expirationTimeTask = Task.Delay(TimeSpan.FromSeconds(30));
var strm = response.GetContentAsStreamAsync();
var finishedTask = await Task.WhenAny(expirationTimeTask, strm);
if (finishedTask == expirationTimeTask)
{
   //timeout error processing (does not get here)
}   

 

sanjayssk
Helpful | Level 6
Go to solution

Oops. On debugging, I found that it is stuck at CopyTo and that takes a long time and times out. So does the actual download occur on "CopyTo"?

 

Update: Was above to solve this by studying the Asp.net samples on:

https://docs.microsoft.com/en-us/aspnet/web-forms/overview/performance-and-caching/using-asynchronou...

yasirjaseem
New member | Level 2
Go to solution

How to call it on windows/desktop application to download file.

Greg-DB
Dropbox Staff
Go to solution

@yasirjaseem We recommend using the official Dropbox .NET SDK. There's an example of downloading files here. If something isn't working as expected for you, open a new thread with details.

kelly0318
New member | Level 2
Go to solution

i am tring to downlaod a maual but it will not work

 

Greg-DB
Dropbox Staff
Go to solution

@kelly0318 This is an old thread about writing code to download from Dropbox programmatically. If you need help using Dropbox itself you may want to open a thread in the 'Help from the Community > Files & folders' section or otherwise contact support.

Need more support?