2016-05-20 72 views
0

下面的代碼將文件上傳到具有相同名稱,大小和文件類型的服務器上(如正在上傳虛擬文件)。但是,我何時嘗試查看它,它會顯示注意事項。當我嘗試上傳.txt文件時,它可以正常工作。怎麼了?使用REST api在Dropbox上上傳文件

public static void UploadFile(string accessToken,string path,HttpPostedFileBase file) 
     { 
      try 
      { 

       var client = new RestClient("https://content.dropboxapi.com/1/files_put/auto/Abc/" + file.FileName); 
       var request = new RestRequest(Method.PUT); 
       request.AddHeader("Authorization", "Bearer " + accessToken); 
       request.AddHeader("Content-Type", file.ContentType); 
       //request.AddHeader("Content-Length", file.ContentLength.ToString());        
       request.AddFile("file", path); 

       IRestResponse response = client.Execute(request); 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
     } 

回答

0

我從班假設我看到你使用RestSharp?我不是那麼熟悉,但從快速搜索,它看起來不像你想要的AddFile。 (這設置了一個多形式上傳,這是Dropbox的API希望不是。)

相反的request.AddFile(...),我想你想是這樣的(經過充分測試):

// Get a byte array of the file content. Note that this involves reading 
// the entire file into memory! I couldn't immediately find a way to work 
// with the stream itself in RestSharp. 
MemoryStream target = new MemoryStream(); 
file.InputStream.CopyTo(target); 
byte[] data = target.ToArray(); 

// Send those bytes as the body of your HTTP request. 
request.AddParameter("application/octet-stream", data, ParameterType.RequestBody); 
+0

謝謝關心。 我已經試過這個,但它沒有奏效。 –

+0

當你嘗試這個時發生了什麼? – smarx

+0

它在那裏創建一個文件,但是當我想要預覽它什麼也不顯示,並且當我檢查文件大小,文件大小在字節 –