简体   繁体   中英

How to post file and data to api using httpclient

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

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.

=> Here I put a link, Try this way it may be helpful for you. Please take look into.

Visit https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM