原地址:http://www.cnblogs.com/greenerycn/archive/2010/05/15/csharp_http_post.html

1、客户端代码 用winform写的

private void button1_Click(object sender, EventArgs e)
{
string postURL = "http://localhost:40694/test1/Handler1.ashx";
string[] files = { "F:\\1.png", "F:\\2.png" };
string str = HttpUploadFile(postURL, files);
MessageBox.Show(str);
} public string HttpUploadFile(string url, string[] files)
{
//HttpContext context
//参考http://www.cnblogs.com/greenerycn/archive/2010/05/15/csharp_http_post.html
var memStream = new MemoryStream(); // 边界符
var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n"); // 文件参数头
foreach (var item in files)
{
string filePath = item;
string fileName = Path.GetFileName(item);
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
var fileHeaderBytes = Encoding.UTF8.GetBytes($"Content-Disposition: form-data; name=\"{"file"}\"; filename=\"{fileName}\"\r\nContent-Type: application/octet-stream\r\n\r\n"); // 开始拼数据
memStream.Write(beginBoundary, , beginBoundary.Length); // 文件数据
memStream.Write(fileHeaderBytes, , fileHeaderBytes.Length);
var buffer = new byte[];
int bytesRead;
while ((bytesRead = fileStream.Read(buffer, , buffer.Length)) != )
{
memStream.Write(buffer, , bytesRead);
}
fileStream.Close(); //必须得写入一个换行
byte[] bytes = Encoding.UTF8.GetBytes($"\r\n");
memStream.Write(bytes, , bytes.Length);
} //// Key-Value数据
//Dictionary<string, string> stringDict = new Dictionary<string, string>();
//stringDict.Add("len", "500");
//stringDict.Add("wid", "300");
//stringDict.Add("test1", "1"); //foreach (var item in stringDict)
//{
// string name = item.Key;
// string value = item.Value;
// byte[] bytes = Encoding.UTF8.GetBytes($"\r\n--{boundary}\r\nContent-Disposition: form-data; name=\"{name}\"\r\n\r\n{value}\r\n");
// memStream.Write(bytes, 0, bytes.Length);
//} // 写入最后的结束边界符
var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");// 最后的结束符
memStream.Write(endBoundary, , endBoundary.Length); //写入到tempBuffer
memStream.Position = ;
var tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, , tempBuffer.Length);
memStream.Close(); // 创建webRequest并设置属性
var webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.Timeout = ;
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
webRequest.ContentLength = tempBuffer.Length; var requestStream = webRequest.GetRequestStream();
requestStream.Write(tempBuffer, , tempBuffer.Length);
requestStream.Close(); var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
string responseContent;
using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("utf-8")))
{
responseContent = httpStreamReader.ReadToEnd();
} httpWebResponse.Close();
webRequest.Abort(); return responseContent;
}

2、服务端代码

 public class Handler1 : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain"; string strFileNames = "";
if (context.Request.Files.Count > )
{
for (int i = ; i < context.Request.Files.Count; i++)
{
HttpPostedFile _HttpPostedFile = context.Request.Files[i];
strFileNames += _HttpPostedFile.FileName + ",";
_HttpPostedFile.SaveAs(context.Server.MapPath($"./{_HttpPostedFile.FileName}"));
}
//context.Response.Write($"files:{context.Request.Files.Count} filenames:{strFileNames}");
}
//context.Response.Write(context.Request.Form["len"]);
//context.Response.Write(JsonConvert.SerializeObject(context.Request.Form.AllKeys.Length.ToString()));
context.Response.Write(strFileNames);
} public bool IsReusable
{
get
{
return false;
}
}
}

C# 模拟多文件上传的更多相关文章

  1. iframe 模拟ajax文件上传and formdata ajax 文件上传

    对于文件上传 有好多种方式,一直想总结 文件上传的方法 今天就来写下 iframe  的文件上传的代码 本人语言表达能里有限,不多说了 直接上代码. 首先看 总体页面. 总共就三个文件. 实际上也就是 ...

  2. python模拟浏览器文件上传,csrf放行

    服务器端视图函数 from django.shortcuts import render,HttpResponse from django.views.decorators.csrf import c ...

  3. AJAX-----11iframe模拟ajax文件上传效果原理3

    如果直接给用户提示上传成功,那么如果用户上传的文件比较大点,那么等上半天都没反映,那么用户很有可能会刷新或者关了从来等... 那么会给我们服务器带来一定的影响,所以我们可以对这方面的用户体验度进行提升 ...

  4. AJAX-----09iframe模拟ajax文件上传效果原理1

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. AJAX-----10iframe模拟ajax文件上传效果原理2

    在实际开发中其实我们可以给用户一些提示,比如上传成功或者上传失败,废话不多说,走码: <!DOCTYPE html> <html lang="en"> &l ...

  6. 在WebBrowser中通过模拟键盘鼠标操控网页中的文件上传控件(转)

    引言 这两天沉迷了Google SketchUp,刚刚玩够,一时兴起,研究了一下WebBrowser. 我在<WebBrowser控件使用技巧分享>一文中曾谈到过“我现在可以通过WebBr ...

  7. Python模拟HTTP Post上传文件

    使用urllib2模块构造http post数据结构,提交有文件的表单(multipart/form-data),本示例提交的post表单带有两个参数及一张图片,代码如下: #buld post bo ...

  8. Netty学习笔记(一):接收nodejs模拟表单上传的文件

    好久不写博客了,也好久不写代码了,这两天临时遇上一个事情,觉得不难,加上觉得手有些生,就动手做了一下,结果遇上了不少坑,有新坑,有老坑,痛苦无比,现在总算差不多了,赶紧记录下来,希望以后不再重复这种痛 ...

  9. layui文件上传进度条(模拟)

    1.修改上传组件js(没测) https://blog.csdn.net/weixin_42457316/article/details/81017471 https://www.cnblogs.co ...

随机推荐

  1. mysql下有符号数和无符号数的相关问题

    最近自己的程序在调用mysql的存储过程传参给smallint类型变量的时候,总是出现out of range value的错误,刚开始用C数值转换方式的二进制位转换思路来思考时,总是觉得没什么问题, ...

  2. django-request获取数据

    request 如果说 urls.py 是 Django 中前端页面和后台程序桥梁,那么 request 就是桥上负责运输的小汽车 可以说后端接收到的来至前端的信息几乎全部来自于requests中. ...

  3. vue和react动画区别

    触发动画 vue触发动画是 v-show,v-if ,动态组件或者组件的根节点 react 是CSSTransition上的属性 in 是true 或false触发动画

  4. windows 下安装 docker

    1. 使用阿里云的镜像进行安装: http://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/ 2. 安装完成后点击图标 “Dock ...

  5. SpringBoot 实现前后端分离的跨域访问(CORS)

    序言:跨域资源共享向来都是热门的需求,使用CORS可以帮助我们快速实现跨域访问,只需在服务端进行授权即可,无需在前端添加额外设置,比传统的JSONP跨域更安全和便捷. 一.基本介绍 简单来说,CORS ...

  6. jquery双击事件会触发单击事件

    实际工作中,我们经常会遇到在同一个元素上,绑定多种事件类型,比较常见的是单击事件和一些鼠标事件,一般而言影响不大.但是如果同时绑定单击事件和双击事件呢? 其实,只要能够想明白的话,解决方案也比较简单, ...

  7. 关于微信小程序更新内容后手机上不能及时显示的办法

    这几天一直在做微信小程序的二次开发,每天都要发布程序,但是发布之后微信上查看小程序和以前的一模一样,丝毫没有改变,但是我再本地上却改变了,而且没有开的不校验合法域名那个.这是为啥呢????? 这是跟小 ...

  8. html调用html的方法

    html调用html的方法 html中引入调用另一个html的方法,尝试了好几种,都列出来: 其中第一种是最好的,其他的方法,可以尝试看看,是不是适合你当前项目 一.div+$(“#page1”).l ...

  9. 使用include重用布局

    尽管Android 支持各种小部件,来提供小且可以重用的交互元素,你可能还需要更大的,要求一个专门布局的重用组件.为了高效的重用整个布局,你能使用和标签在当前的布局中嵌入别的布局. 重用布局功能特别强 ...

  10. 白鹭引擎 - 矢量绘图 ( graphics )

    class Main extends egret.DisplayObjectContainer { /** * Main 类构造器, 初始化的时候自动执行, ( 子类的构造函数必须调用父类的构造函数 ...