2013-04-17 58 views
0

我已經創建了一個web api,通過OAuth將用戶連接到Dropbox。我正在使用API​​與Dropbox進行交互,Dropbox會根據需要在本地進行交互,但是當我將API部署到Azure服務器時,我無法下載。我預料到會發生這種情況,因爲我的API目前正在被硬編碼到我的機器上。 這裏是我使用的方法: 注:我通過一個ActionResult調用此方法,因爲我的項目下載功能MVC 4

public FileSystemInfo DownloadFile(string root, string path) 
     { 
      var uri = new Uri(new Uri(DropboxRestApi.ApiContentServer), 
       String.Format("files?root={0}&path={1}", 
       root, UpperCaseUrlEncode(path))); 

      var oauth = new OAuth(); 
      var requestUri = oauth.SignRequest(uri, _consumerKey, _consumerSecret, _accessToken); 

      var request = (HttpWebRequest) WebRequest.Create(requestUri); 
      request.Method = WebRequestMethods.Http.Get; 
      var response = request.GetResponse(); 

      var metadata = response.Headers["x-dropbox-metadata"]; 
      var file = ParseJson<FileSystemInfo>(metadata); 

      using (Stream responseStream = response.GetResponseStream()) 
      using (MemoryStream memoryStream = new MemoryStream()) 
      { 
       byte[] buffer = new byte[1024]; 
       int bytesRead; 
       do 
       { 
        bytesRead = responseStream.Read(buffer, 0, buffer.Length);      
        memoryStream.Write(buffer, 0, bytesRead); 
       } while (bytesRead > 0); 

       file.Data = memoryStream.ToArray(); 
      }          
      return file;          
     } 

的MVC部分的一部分,這就是我所說的方法在我的行動的結果。

var file = api.DownloadFile("dropbox", "Public/downloadThis.jpg"); 
        path = file.Path; 
        file.Save(@"....\Desktop\DemoTest\Downloads\downloadThis.jpg"); --- this is the problem & *Save* is a stream writer 

從瀏覽器上的服務器上下載文件時是否需要遵循以下步驟?

+0

在這裏看到:http://stackoverflow.com/a/5830215 – haim770

回答

0

公衆的ActionResult下載(Models.downloadModel dowld,Models.LoggerView日誌) {

 string TC_ID = Request.QueryString["id"].ToString(); 
     string filename = TC_ID+"_LoggerData" + ".zip"; 
     Response.ContentType = "application/octet-stream"; 
     Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename); 
     Response.TransmitFile(Server.MapPath("~/files/" + filename)); 
     Response.End(); 

}