public class CreateBytes
{
Encoding encoding = Encoding.UTF8; /**/
/// <summary>
/// 拼接所有的二进制数组为一个数组
/// </summary>
/// <param name="byteArrays">数组</param>
/// <returns></returns>
/// <remarks>加上结束边界</remarks>
public byte[] JoinBytes(ArrayList byteArrays)
{
int length = ;
int readLength = ; // 加上结束边界
string endBoundary = Boundary + "--\r\n"; //结束边界
byte[] endBoundaryBytes = encoding.GetBytes(endBoundary);
byteArrays.Add(endBoundaryBytes); foreach (byte[] b in byteArrays)
{
length += b.Length;
}
byte[] bytes = new byte[length]; // 遍历复制
//
foreach (byte[] b in byteArrays)
{
b.CopyTo(bytes, readLength);
readLength += b.Length;
} return bytes;
} public bool UploadData(string uploadUrl, byte[] bytes, out byte[] responseBytes)
{
WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", ContentType); try
{
responseBytes = webClient.UploadData(uploadUrl, bytes);
return true;
}
catch (WebException ex)
{
Stream resp = ex.Response.GetResponseStream();
responseBytes = new byte[ex.Response.ContentLength];
resp.Read(responseBytes, , responseBytes.Length);
}
return false;
} /**/
/// <summary>
/// 获取普通表单区域二进制数组
/// </summary>
/// <param name="fieldName">表单名</param>
/// <param name="fieldValue">表单值</param>
/// <returns></returns>
/// <remarks>
/// -----------------------------7d52ee27210a3c\r\nContent-Disposition: form-data; name=\"表单名\"\r\n\r\n表单值\r\n
/// </remarks>
public byte[] CreateFieldData(string fieldName, string fieldValue)
{
string textTemplate = Boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n";
string text = String.Format(textTemplate, fieldName, fieldValue);
byte[] bytes = encoding.GetBytes(text);
return bytes;
} /**/
/// <summary>
/// 获取文件上传表单区域二进制数组
/// </summary>
/// <param name="fieldName">表单名</param>
/// <param name="filename">文件名</param>
/// <param name="contentType">文件类型</param>
/// <param name="contentLength">文件长度</param>
/// <param name="stream">文件流</param>
/// <returns>二进制数组</returns>
public byte[] CreateFieldData(string fieldName, string filename, string contentType, byte[] fileBytes)
{
string end = "\r\n";
string textTemplate = Boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n"; // 头数据
string data = String.Format(textTemplate, fieldName, filename, contentType);
byte[] bytes = encoding.GetBytes(data); // 尾数据
byte[] endBytes = encoding.GetBytes(end); // 合成后的数组
byte[] fieldData = new byte[bytes.Length + fileBytes.Length + endBytes.Length]; bytes.CopyTo(fieldData, ); // 头数据
fileBytes.CopyTo(fieldData, bytes.Length); // 文件的二进制数据
endBytes.CopyTo(fieldData, bytes.Length + fileBytes.Length); // \r\n return fieldData;
} #region 属性
public string Boundary
{
get
{
string[] bArray, ctArray;
string contentType = ContentType;
ctArray = contentType.Split(';');
if (ctArray[].Trim().ToLower() == "multipart/form-data")
{
bArray = ctArray[].Split('=');
return "--" + bArray[];
}
return null;
}
} public string ContentType
{
get
{
if (HttpContext.Current == null)
{
return "multipart/form-data; boundary=---------------------------7d5b915500cee";
}
return HttpContext.Current.Request.ContentType;
}
}
#endregion
}

调用方式如下:

CreateBytes cb = new CreateBytes();
// 所有表单数据
ArrayList bytesArray = new ArrayList();
// 普通表单
bytesArray.Add(cb.CreateFieldData("jsonDataStr", "")); // 读文件流
FileStream fs = new FileStream("wolfy.JPG", FileMode.Open,
FileAccess.Read, FileShare.Read); string ContentType = "application/octet-stream";
byte[] fileBytes = new byte[fs.Length];
fs.Read(fileBytes, , Convert.ToInt32(fs.Length)); // 文件表单
bytesArray.Add(cb.CreateFieldData("file1", "wolfy.JPG", ContentType, fileBytes)); bytesArray.Add(cb.CreateFieldData("file2", "wolfy1.JPG", ContentType, fileBytes));
// 合成所有表单并生成二进制数组
byte[] bytes = cb.JoinBytes(bytesArray); // 返回的内容
byte[] responseBytes; bool uploaded = cb.UploadData("http://localhost:8700/api/System/ImgUpload", bytes, out responseBytes);

WebClient 实现多文件/文本同时上传的更多相关文章

  1. 文件无刷新上传(swfUpload与uploadify)

    文件无刷新上传并获取保存到服务器端的路径 遇到上传文件的问题,结合之前用到过的swfUpload,又找了一个无刷新上传文件的jquery插件uploadify,写篇博客记录一下分别介绍这两个插件的实现 ...

  2. html5 文件拖拽上传

    本文首先发表在  码蜂笔记 : http://coderbee.net/index.php/web/20130703/266 html5 文件拖拽上传是个老话题了,网上有很多例子,我一开始的代码也是网 ...

  3. 【Android实战】----基于Retrofit实现多图片/文件、图文上传

    本文代码详见:https://github.com/honghailiang/RetrofitUpLoadImage 一.再次膜拜下Retrofit Retrofit不管从性能还是使用方便性上都非常屌 ...

  4. Java web开发——文件夹的上传和下载

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...

  5. 需求-java web 能够实现整个文件夹的上传下载吗?

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...

  6. Java实现FTP文件与文件夹的上传和下载

    Java实现FTP文件与文件夹的上传和下载 FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文传协议".用于Internet上的控制 ...

  7. [New Portal]Windows Azure Virtual Machine (15) 在本地制作数据文件VHD并上传至Azure(2)

    <Windows Azure Platform 系列文章目录> 在上一章内容里,我们已经将包含有OFFICE2013 ISO安装文件的VHD上传至Azure Blob Storage中了. ...

  8. WEB版一次选择多个文件进行批量上传(Plupload)的解决方案

    WEB版一次选择多个文件进行批量上传(Plupload)的解决方案  转载自http://www.cnblogs.com/chillsrc/archive/2013/01/30/2883648.htm ...

  9. apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出

    用到了apache 的 commons-net-3.0.1.jar 和 log4j-1.2.15.jar 这连个jar包 JAVA 代码如下: package com.bjut.edu.cn.ftp; ...

随机推荐

  1. android SharedPreferences 存储对象

    我们知道SharedPreferences只能保存简单类型的数据,例如,String.int等. 如果想用SharedPreferences存取更复杂的数据类型(类.图像等),就需要对这些数据进行编码 ...

  2. [LeetCode] Arranging Coins 排列硬币

    You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...

  3. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  4. textarea 中的 innerHTML 和 value

    <textarea></textarea> <input type="button" value="click" /> &l ...

  5. Suspend to RAM和Suspend to Idle分析,以及在HiKey上性能对比

    Linux内核suspend状态 Linux内核支持多种类型的睡眠状态,通过设置不同的模块进入低功耗模式来达到省电功能.目前存在四种模式:suspend to idle.power-on standb ...

  6. RTMP流媒体播放过程

      RTMP协议规定:第一步,建立一个网络连接(NetConnection):客户端和服务端的基础连通关系 第二步:建立一个网络流(NetStream)发送多媒体的通道(只能建立一个网络连接,可以建立 ...

  7. Python版设计模式: 创建型模式:单例模式和工厂模式家族

    一. 单例模式(Singleton) 所谓单例模式,也就是说不管什么时候都要确保只有一个对象实例存在.很多情况下,整个系统中只需要存在一个对象,所有的信息都从这个对象获取,比如系统的配置对象,或者是线 ...

  8. iOS10推送通知适配

    iOS10推送新增了UserNotifications Framework,使用起来其实很简单. 只是在iOS10以上系统上点击通知栏,回调方法不再走原来的这两个方法 - (void)applicat ...

  9. FragmentTabHost简单保存状态的方法

    private View rootView;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container ...

  10. 虚拟机下Centos7如何设置静态IP地址

    最近在学习linux环境部署~~~~ 首先,将网络适配设置成为桥接模式 查看本机IP地址,ipconfig,记住ipv4地址和默认网关地址,等会配置的时候要用 启动Centos,进入终端模式,设置IP ...