0

I am at learning phase and i want to post file and data to api using httpclient. i have tried this. Here is my controller code

string baseUrl = ServerConfig.server_path + "/api/Payment/AddMedicineOrder"; Dictionary parameters = new Dictionary();

        parameters.Add("username",user.Username);
        parameters.Add("FullName", FullName);
        parameters.Add("Phone", Phone);
        parameters.Add("CNIC", CNIC);
        parameters.Add("address", address);
        parameters.Add("Email", Email);
        parameters.Add("dateofbirth", dateofbirth.ToShortDateString());
        parameters.Add("Gender", Gender);
        parameters.Add("PaymentMethod", PaymentMethod);
        parameters.Add("Title", Title);
        parameters.Add("PhramaList", medList);



       HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("https://localhost:44391/");
            MultipartFormDataContent form = new MultipartFormDataContent();
            HttpContent content = new StringContent("fileToUpload");
            HttpContent DictionaryItems = new FormUrlEncodedContent(parameters);
            form.Add(content, "fileToUpload");
            form.Add(DictionaryItems, "medicineOrder");
   var stream = PostedPrescription.InputStream;
            content = new StreamContent(stream);
            content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                Name = "fileToUpload",
                FileName = PostedPrescription.FileName
            };
            form.Add(content);

            var response =await client.PostAsync("/api/Payment/AddMedicineOrder", form);
            var k =response.Content.ReadAsStringAsync().Result;

How to pass this in Web api Method

   [HttpPost]
            public async Task<API> AddMedicineOrder()//string key, [FromUri]MedicineOrder medicineOrder
            {
                var request = HttpContext.Current.Request;
                bool SubmittedFile = (request.Files.Count != 0);
                this.Request.Headers.TryGetValues("medicineOrder", out IEnumerable<string> somestring);

                var k = somestring;
  return OK("Success");
            }
            catch (Exception ex)
            {
                return InternalServerError("Technical Error.");
            }

please help me. Thanks in advance

4
  • 1
    So what is not working? Commented Feb 6, 2018 at 11:32
  • 1
    Your Web API action doesn't accept parameters. Either add all 11 parameters to the action or create an object with those 11 properties. Don't try to read the parameters from the Request Commented Feb 6, 2018 at 11:34
  • if i add parameters (i have a class that has all the parameters) to web api. it gives null. I am only reading the data this way because the file does not get submitted. i get file in Request in this way. Commented Feb 6, 2018 at 11:42
  • Try to convert the file contents into byte[] (for example, by using File.ReadAllBytes(...). If that doesn't work, try converting byte[] into Base64String (i.e., Convert.Base64String(byte[] object)), and after that post that file to the service via httpClient Commented Feb 6, 2018 at 11:55

1 Answer 1

1

You need to support add multipart/form-data support to your web api. For this you can add custom media type formatter which will read your json content, as well as file content and you can bind that to a concrete model directly.

Sign up to request clarification or add additional context in comments.

Comments

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.