Search code examples
c#dropbox-apiwindows-10-universal

Download PNG file to local machine using Dropbox.NET on UWP platform


I am able to upload PNG image to Dropbox folder however, I don't know how to download the PNG (or other images) from Dropbox. What I get from the tutorial page is:

async Task Download(DropboxClient dbx, string folder, string file)
{
    using (var response = await dbx.Files.DownloadAsync(folder + "/" + file))
    {
        Console.WriteLine(await response.GetContentAsStringAsync());
    }
}

Do anyone have the sample code for downloading file to local drive? Thanks.


Solution

  • The Dropbox API .NET SDK DownloadAsync method gives you a IDownloadResponse which offers three methods for getting the file content:

    • GetContentAsByteArrayAsync
    • GetContentAsStreamAsync
    • GetContentAsStringAsync

    For example, to save the file content to a local file, you can do something like:

    public async Task Download(string remoteFilePath, string localFilePath)
    {
        using (var response = await client.Files.DownloadAsync(remoteFilePath))
        {
            using (var fileStream = File.Create(localFilePath))
            {
                response.GetContentAsStreamAsync().Result.CopyTo(fileStream);
            }
    
        }
    }
    

    That would download the file content from a file at remote Dropbox file path remoteFilePath to the local path localFilePath.