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(""), "id");
content.Add(new StringContent("wjire"), "name");
content.Add(new StringContent(""), "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 > ) {
foreach (string file in files) {
var img = files[file];
if (img?.ContentLength > ) {
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 > ) {
var bytes = new byte[stream.Length];
stream.Read(bytes, , bytes.Length);
}
}
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "没有文件");
}

最新文章

  1. 将 instance 部署到 OVS Local Network - 每天5分钟玩转 OpenStack(130)
  2. “ExternalException (0x80004005): GDI+ 中发生一般性错误”的问题 .
  3. springmvc中request的线程安全问题
  4. nodejs学习笔记&lt;四&gt;处理请求参数
  5. Oracle 基础 &lt;2&gt; --函数
  6. Codeforces Gym 100500F Problem F. Door Lock 二分
  7. 数据结构———KMP
  8. 【HTML】Beginner9:Form
  9. MySQL快捷键
  10. 《傲慢与偏见》(Pride and Prejudice)
  11. 后缀数组da3模板
  12. erlang shell表格数据对齐
  13. c# BinaryWriter 和 BinaryReader
  14. [SCOI 2010]传送带
  15. Kapacitor之TICK脚本,监控输出
  16. ASP.NET -- WebForm -- HttpResponse 类的方法和属性
  17. centos7 多网卡绑定bond0 之mod4
  18. js模拟散列
  19. mysql创建索引-----高性能(五)
  20. hihocoder#1046 K个串 可持久化线段树 + 堆

热门文章

  1. rabbitmq之rpc
  2. 孤荷凌寒自学python第五十六天通过compass客户端和mongodb shell 命令来连接远端MongoDb数据库
  3. 孤荷凌寒自学python第四十六天开始建构自己用起来更顺手一点的Python模块与类尝试第一天
  4. 安装cloudbase-init和qga批处理
  5. OZ customize windows iamge
  6. 上手Caffe(一)
  7. 软工实践 - 第十一次作业 Alpha 冲刺 (3/10)
  8. Python读写tap设备
  9. 轻松精通awk数组企业问题案例
  10. 【bzoj2212】[Poi2011]Tree Rotations 权值线段树合并