.NET Zone is brought to you in partnership with:

I started out writing Windows applications using Delphi and websites using PHP back in 2001. In 2007 I made the switch to the .NET framework and haven't looked back since. Currently I am working as a .NET developer for Euricom in Belgium. Christophe is a DZone MVB and is not an employee of DZone and has posted 23 posts at DZone. You can read more from them at their website. View Full User Profile

How to download a file from Dropbox using the Dropbox .NET wrapper

  • submit to reddit

So far we’ve covered the following topics in the Dropbox series:

Once you are authenticated you can make API requests such as requesting your account information, creating, deleting folders…etc. One particular type of request is downloading a file from your Dropbox account. Once you’ve worked your way through the previous 3 parts this becomes trivially easy.

If you want to follow along go to the download page and download the code for the third part (article #65). Unzip it and open it up in Visual Studio.

A New Method

Let’s add a new method to the DropboxApi type called DownloadFile(…).

public FileSystemInfo DownloadFile(string root, string path)
    // ...

It takes two parameters, namely:

  • root: The root relative to which the path is specified. Valid values are sandbox and dropbox.
  • path: The path to the file you want to download.

It returns an instance of the FileSystemInfo type, which we created earlier in part #3. The FileSystemInfo type contains the file’s metadata.

Example usage of the new method:

var api = new DropboxApi(ConsumerKey, ConsumerSecret, accessToken);
var file = api.DownloadFile("dropbox", "Public/DropboxPart3.zip");

Top of page

Expanding FileSystemInfo

Before we can proceed we need to modify the existing FileSystemInfo type.

public class FileSystemInfo

    public byte[] Data { get; internal set; }

    public void Save(string path)
        using (var fileStream = new FileStream(
            path, FileMode.Create, FileAccess.ReadWrite))
            fileStream.Write(Data, 0, Data.Length);

The new property Data returns a byte array which contains the contents of the file we downloaded. The Save(…) method allows you to easily save the file. No world shocking news here.

Top of page

Downloading the File

Let’s implement the DownloadFile(…) method. When downloading a file we need to contact the API content server. So let’s add a URL to the DropboxRestApi type.

public class DropboxRestApi
    public const string ApiVersion = "1";

    public const string ApiContentServer =
        "https://api-content.dropbox.com/" + ApiVersion + "/";


Now we can start with implementing the DownloadFile(…) method. First we need to compose a URL for the file which we want to retrieve.

For example:


var uri = new Uri(new Uri(DropboxRestApi.ApiContentServer),
    root, UpperCaseUrlEncode(path)));

Remark: We created the UpperCaseUrlEncode(…) method last time to circumvent a rather annoying problem when URL encoding the path.

Next we need to sign the request.

var oauth = new OAuth();
var requestUri = oauth.SignRequest(uri, _consumerKey, _consumerSecret,

Then you can finally execute a GET request to download the file.

var request = (HttpWebRequest) WebRequest.Create(requestUri);
request.Method = WebRequestMethods.Http.Get;
var response = request.GetResponse();

The HTTP response contains the file’s metadata in JSON format within an x-dropbox-metadata header. Let’s extract this metadata from the headers and deserialize it into a FileSystemInfo instance.

var metadata = response.Headers["x-dropbox-metadata"];
var file = ParseJson<FileSystemInfo>(metadata);

Now we can read the response and store the file as a byte array in the FileSystemInfo instance.

using (Stream responseStream = response.GetResponseStream())
using (MemoryStream memoryStream = new MemoryStream())
    byte[] buffer = new byte[1024];
    int bytesRead;
        bytesRead = responseStream.Read(buffer, 0, buffer.Length);
        memoryStream.Write(buffer, 0, bytesRead);
    } while (bytesRead > 0);

    file.Data = memoryStream.ToArray();

And the only thing that remains is to return our FileSystemInfo instance.

return file;

Voila, that’s the whole implementation of the DownloadFile(…) method.

Thanks to the fact that we did most of the work during the previous parts, this turned out to be a rather short article. Check out the Dropbox REST API documentation if you want to explore it further and implement new features on your own.


In part 5 I’ll discuss how you can upload new files to your Dropbox account. You can download the source code accompanying this article from the download page. If you have any questions or suggestions please drop me an e-mail or submit a comment.

Published at DZone with permission of Christophe Geers, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Ajya Chang replied on Mon, 2012/02/27 - 12:33am



Thanks for this article. It is really helpful .Your article explains the downloading of a file using .NET wrapper with good eample and simplicity. It can be easily understood. Can these code be converted to a class and used across the application or can we use it to create our own api?. Looking forward to hear from you.



Kookee Gacho replied on Wed, 2012/06/06 - 8:08am

According to Dropbox, founder Drew Houston conceived the idea after repeatedly forgetting his USB flash drive while he was a student at MIT. He says that existing services at the time "suffered problems with Internet latency, large files, bugs, or just made me think too much." He began making something for himself, but then realized that it could benefit others with the same problem.-Dr. Marla Ahlgrimm

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.