Download file from Dropbox C# [duplicate]

大憨熊 提交于 2019-12-11 05:04:37

问题


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.


回答1:


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(string 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);
        }
    }
}


来源:https://stackoverflow.com/questions/39881803/download-file-from-dropbox-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!