• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#HttpClientPost参数同时上传文件上传图片调用接口

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
// 调用接口上传文件
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:\test.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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#函数式程序设计之用闭包封装数据发布时间:2022-07-10
下一篇:
C#拦截系统消息的方法Application.AddMessageFilter发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap