zoukankan      html  css  js  c++  java
  • HttpClient 同时上传多个文件及参数, 同时利用 Web Api 接收

                using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient()) {
    
                    client.BaseAddress = new Uri("http://192.168.1.3:42561");
                    string api = "api/upload/uploadpost";
    
                    MultipartFormDataContent content = new MultipartFormDataContent();
                    string path1 = @"H:图片1.jpg";
                    string path2 = @"H:图片2.jpg";
                    FileStream fs1 = new FileStream(path1, FileMode.Open, FileAccess.Read);
                    FileStream fs2 = new FileStream(path2, FileMode.Open, FileAccess.Read);
                    content.Add(new StreamContent(fs1), "myFile1", Guid.NewGuid() + ".jpg");
                    content.Add(new StreamContent(fs2), "myFile2", Guid.NewGuid() + ".jpg"); 
    
                    //还可以添加键值对参数,web api 可以通过 request.Form["id"] 接收
                    content.Add(new StringContent("1"), "id");
                    content.Add(new StringContent("wjire"), "name");
                    content.Add(new StringContent("33"), "age");
    
                    var result = client.PostAsync(api, content).Result;
                    var str = result.Content.ReadAsStringAsync().Result;
                    fs1.Dispose();
                    fs2.Dispose();
                    Console.WriteLine(str);
                }

    后台接收:

            public async Task<HttpResponseMessage> UploadPost() {
                var request = HttpContext.Current.Request;
                var id = request.Form["id"];
                var name = request.Form["name"];
                var age = request.Form["age"];
                var files = HttpContext.Current.Request.Files;
                var path = HttpContext.Current.Server.MapPath("/img/");
                if (files.Count > 0) {
                    foreach (string file in files) {
                        var img = files[file];
                        if (img?.ContentLength > 0) {
                            var fileName = img.FileName;
                            await Task.Run(() => img.SaveAs(path + fileName));
                        }
                    }
                    return new HttpResponseMessage(HttpStatusCode.OK)
                    {
                         Content = new StringContent("成功@!!!!!")
                    };
                } else {
                    var stream = request.InputStream;
                    if (stream.Length > 0) {
                        var bytes = new byte[stream.Length];
                        stream.Read(bytes, 0, bytes.Length);
                    }
                }
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "没有文件");
            }
  • 相关阅读:
    System.Configuration引用后ConfigurationManager方法用不了
    HTTP 错误 500.23
    slide ——首尾相接の平滑切换效果
    函数式编程初探之回调
    Call & Apply. It's easy!
    【W3C】 CSS3选择器
    再谈原型和原型链
    ECMA学习小结(3)——constructor 和 prototype
    ECMA学习小结(2)——一切皆对象
    ECMA学习小结(1)——什么是ECMAScript,JAVAScript与ECMA的关系
  • 原文地址:https://www.cnblogs.com/refuge/p/8537094.html
Copyright © 2011-2022 走看看