AlexGH AlexGH - 1 month ago 51
ASP.NET (C#) Question

Download file from Dropbox C#

I'm trying to download a pdf file that I have in Dropbox, I need to save it into my local computer, any folder it can be

C:\Users\User\Desktop
for example.
This is the code I've been working with:

public void DownloadPdf()
{

DropboxClient client2 = new DropboxClient("cU5M-asdgfsdfsdfds3434435dfgfgvXoAMCFyOXH");
string folder = "MyFolder";
string file = "Test PDF.pdf";
var response = client2.Files.DownloadAsync("/" + folder + "/" + file);
}


How can I save that file into my local drive? What do I need to do next? It doesn't throw any error but I'm not even sure that path is going into the pdf document in Dropbox. I'm using Dropbox.Api within ASP.net Core.

Answer

What you need to do is convert the content to a stream and download to your local path. You can do that like so

public void DownloadPdf(localFilePath)
{
    DropboxClient client2 = new DropboxClient("cU5M-asdgfsdfsdfds3434435dfgfgvXoAMCFyOXH");
    string folder = "MyFolder";
    string file = "Test PDF.pdf";
    using (var response = await client.Files.DownloadAsync("/" + folder + "/" + file))
    {
        using (var fileStream = File.Create(localFilePath))
        {
            (await response.GetContentAsStreamAsync()).CopyTo(fileStream);
        }
    }
}
Comments