0

I need to send file to WEB API using HttpClient and using the below code to achieve it. I couldn't figure out why am not revcieving any files in WEB API, but the request hits the APIcontroller.

Client Side Code

            var fs = new FileStream(@"C:\SomLocation\ExcelFile.xlsx", FileMode.Open, FileAccess.Read);
            MemoryStream ms = new MemoryStream();
            fs.CopyTo(ms);
            var client = new HttpClient();                
            client.BaseAddress = new Uri(GetAPIBaseAddress());
            HttpContent content = new StreamContent(ms);
            var data = new MultipartFormDataContent();  
            data.Add(content, "file1");
            var response = client.PostAsync(GetImportAPIURL(), data).Result;

In API

       System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
       foreach(System.Web.HttpPostedFile file in hfc)
       {
           // do some processing;
       }

Any help would be greatly appreciated.

6
  • you better send it as a byte array instead of MultipartFormDataContent Commented Jul 21, 2016 at 11:23
  • @FrebinFrancis - I tried sending it as ByteArrayContent but still it is of no use. Commented Jul 21, 2016 at 11:33
  • instead of byte array content you can create a model and create a property with datatype byte[] and post value into that byte array Commented Jul 21, 2016 at 11:34
  • or if you want to use the current method then refer this link dotnetcodr.com/2013/01/10/… Commented Jul 21, 2016 at 11:36
  • Check this one: stackoverflow.com/questions/31003849/… Commented Jul 21, 2016 at 11:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.