从Dropbox C#下载文件 [英] Download file from Dropbox C#

查看:91
本文介绍了从Dropbox C#下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试下载Dropbox中的pdf文件,我需要将其保存到本地计算机中,该文件夹可以是 C:\Users\User\Desktop
这是我一直在使用的代码:

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

如何将文件保存到本地驱动器?接下来我需要做什么?它不会引发任何错误,但我什至不确定该路径是否将进入Dropbox的pdf文档中。我正在ASP.net Core中使用Dropbox.Api。

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.

推荐答案

您需要做的就是将内容转换为流并下载到您的本地路径。您可以这样操作

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

这篇关于从Dropbox C#下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆