简体   繁体   English

如何使用httpclient将文件和数据发布到api

[英]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.我正处于学习阶段,我想使用 httpclient 将文件和数据发布到 api。 i have tried this.我试过这个。 Here is my controller code这是我的控制器代码

string baseUrl = ServerConfig.server_path + "/api/Payment/AddMedicineOrder"; string baseUrl = ServerConfig.server_path + "/api/Payment/AddMedicineOrder"; Dictionary parameters = new Dictionary();字典参数 = 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如何在 Web api 方法中传递这个

   [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. 您需要支持向Web API添加multipart / form-data支持。 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. 为此,您可以添加自定义媒体类型格式化程序,该格式程序将读取您的json内容以及文件内容,并且可以将其直接绑定到具体模型。

=> 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 访问https://docs.microsoft.com/zh-cn/aspnet/web-api/overview/advanced/sending-html-form-data-part-2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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