zoukankan      html  css  js  c++  java
  • C# HttpClient Post 参数同时上传文件 上传图片 调用接口

    // 调用接口上传文件
    using (var client = new HttpClient())
    {
        using (var multipartFormDataContent = new MultipartFormDataContent())
        {
            var values = new[]
            {
                new KeyValuePair<string, string>("a", "3"),
                new KeyValuePair<string, string>("c", "2"),
                new KeyValuePair<string, string>("d", "2")
                 //other values
            };
     
            foreach (var keyValuePair in values)
            {
                multipartFormDataContent.Add(new StringContent(keyValuePair.Value),
                    String.Format(""{0}"", keyValuePair.Key));
            }
     
            multipartFormDataContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(@"D:	est.jpg")),
                ""pic"",
                ""test.jpg"");
     
            var requestUri = "http://localhost:8080";
            var html = client.PostAsync(requestUri, multipartFormDataContent).Result.Content.ReadAsStringAsync().Result;
        }
    }
    

    API 接口,接收请求参数 和上传的文件

       public string UploadHeadImg()
            {
    
                var ts = HttpContext.Current.Request["d"];
                HttpPostedFile file = System.Web.HttpContext.Current.Request.Files[0];
                // 文件扩展名
                string fileExtension = Path.GetExtension(file.FileName).ToLower();    
        
                Stream sr = file.InputStream;//头像文件流
                Bitmap bitmap = (Bitmap)Bitmap.FromStream(sr);
                bitmap.Save(“c:/img/1.jpg”, System.Drawing.Imaging.ImageFormat.Jpeg);
              return "success";
            }
    

    来源:https://blog.csdn.net/luofeng0710/article/details/84202008

  • 相关阅读:
    CIL中间语言浅谈
    keepalived实现nginx高可用
    centos 开启关闭网卡
    CentOS7下安装httpd服务
    freeswitch socket连接报错
    centos安装nodejs
    CronTrigger
    mysql 表增加索引
    FreeSWITCH呼叫参数之sip_cid_type
    中继网关开启呼入
  • 原文地址:https://www.cnblogs.com/davies/p/10253329.html
Copyright © 2011-2022 走看看