c#Dropbox RestSharp下载文件到流

时间:2015-12-09 15:55:26

标签: c# dropbox dropbox-api restsharp

我有一个'ASP.NET'控制台应用程序,我为Dropbox使用'RestSharp'客户端。

我使用此代码下载文件:

split(dat, c(0, cumsum(dat$event=="I3"))[-(nrow(dat)+1)])

$`0`
  time event  actor
1    1    FA   John
2    2    EX   Alex
3    3    I1   John
4    4    FA   Alex
5    5    FA    Tim
6    6    I3 Sandra

$`1`
   time event actor
7     7    EX  Sara
8     8    EX  John
9     9    EX Eliza
10   10    I3  Alex

当调用 var baseUrl = "https://content.dropboxapi.com"; var client = new RestClient(baseUrl); client.Authenticator = OAuth1Authenticator.ForRequestToken(mc_apiKey, mc_appsecret); RestRequest request = new RestRequest(string.Format("/{0}/files/auto", mc_version), Method.GET); client.Authenticator = OAuth1Authenticator.ForProtectedResource(mc_apiKey, mc_appsecret, accessToken.Token, accessToken.Secret); request.AddParameter("path", path); var responseAccount = client.Execute(request); var fileString = responseAccount.Content; byte[] b1 = System.Text.Encoding.UTF8.GetBytes (fileString); 时,整个文件被加载到内存中,所以当我在Dropbox中有一个非常大的文件时,程序会崩溃。

我需要在不使用client.Execute(request)下载到本地存储的情况下传输文件。

我需要能够以块的形式传输文件。

2 个答案:

答案 0 :(得分:2)

您可以像这样设置request.ResponseWriter:

  var baseUrl = "https://content.dropboxapi.com";

  var client = new RestClient(baseUrl);
  client.Authenticator = OAuth1Authenticator.ForRequestToken(mc_apiKey,mc_appsecret);

  RestRequest request = new RestRequest(string.Format("/{0}/files/auto", mc_version), Method.GET);

  client.Authenticator = OAuth1Authenticator.ForProtectedResource(mc_apiKey, mc_appsecret, accessToken.Token, accessToken.Secret);
  request.AddParameter("path", path);

  string tempFile = Path.GetTempFileName();
  using(var stream = File.Create(tempFile, 1024, FileOptions.DeleteOnClose ))
  {
    request.ResponseWriter = (responseStream) => responseStream.CopyTo(stream);
    var response = client.DownloadData(request);
  }

您可以在文档here

中查看示例

答案 1 :(得分:-2)

我在link找到了最佳答案:

 string url = String.Format("https://content.dropboxapi.com/1/files/auto{0}?oauth_consumer_key={1}&oauth_token={2}&oauth_signature={3}%26{4}", path, app-key, access-token, app-secret, access-token-secret);
 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
 webRequest.Method = "Get";
 WebResponse webResponse = null;
  try
  {
     webResponse = webRequest.GetResponse();
     return webResponse.GetResponseStream();
  }
  catch (Exception ex)
  {
    throw ex;       
  }