2016-04-26 143 views
0

我的手機廠商都在向我發送以下請求上傳文件multiipart表單數據請求..如何處理REST API

var client = new RestClient(URL); 
var request = new RestRequest(Method.POST); 
request.AddHeader("postman-token", "52606652-   f26a-e809-0de1-97a6030097eb"); 
request.AddHeader("cache-control", "no-cache"); 
request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001"); 
request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent- Disposition: form-data; name``=\"attachement\"; filename=\"[object Object]\"\r\nContent-Type: false\r\n\r\n\r\n-----011000010111000001101001--", ParameterType.RequestBody); 
IRestResponse response = client.Execute(request); 

請誰能告訴我如何編寫代碼,上傳其餘文件API c#。

回答

0

你可以閱讀到多,從貼HttpContext的這樣的數據:

[HttpPost] 
    public async Task<HttpResponseMessage> UploadDoc() 
    { 
     try 
     { 

      string root = HttpContext.Current.Server.MapPath("~/App_Data"); 
      var provider = new MultipartFormDataStreamProvider(root); 
      if (System.Web.HttpContext.Current.Request.Files.Count > 0) 
      { 
       foreach (string file in System.Web.HttpContext.Current.Request.Files) 
       { 
        byte[] postedFile = null; 
        using (var binaryReader = new BinaryReader(System.Web.HttpContext.Current.Request.Files[file].InputStream)) 
        { 
         postedFile = binaryReader.ReadBytes(System.Web.HttpContext.Current.Request.Files[file].ContentLength); 

        } 
        string fileName = System.Web.HttpContext.Current.Request.Files.AllKeys[0]; 
       } 
      } 
      await Request.Content.ReadAsMultipartAsync(provider); 
+0

我得到的請求是空..請建議 – Mehak