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. TCP/IP协议(一)网络基础知识

    参考书籍为<图解tcp/ip>-第五版.这篇随笔,主要内容还是TCP/IP所必备的基础知识,包括计算机与网络发展的历史及标准化过程(简述).OSI参考模型.网络概念的本质.网络构建的设备等 ...

  2. CSS布局

    1流动模型 先来说一说流动模型,流动(Flow)是默认的网页布局模式.也就是说网页在默认状态下的 HTML 网页元素都是根据流动模型来分布网页内容的. 流动布局模型具有2个比较典型的特征: 第一点,块 ...

  3. 使用SharpZipLib实现文件压缩、解压

    接口 public interface IUnZip { /// <summary> /// 功能:解压zip格式的文件. /// </summary> /// <par ...

  4. ASE周会记录

    本周Sprint Master Atma Hou 一. 本周会议概要 本次会议的主要任务是明确和老师讨论后的数据库设计定稿,同时为我们接下来的连接工作确定包含实现细节的story和接口. 二. 会议内 ...

  5. 项目中angular js的接口url统一管理

    为了防止环境改变时需要修改多处接口的url,项目中用到了一个config.json文件来统一管理url: 在src下建立config文件夹,创建config.json文件,主要内容如下: { &quo ...

  6. 传统瀑布式&敏捷开发

    ---传统瀑布式 瀑布模型式是最典型的预见性的方法,严格遵循预先计划的需求.分析.设计.编码.测试的步骤顺序进行. 步骤成果作为衡量进度的方法,例如需求规格,设计文档,测试计划和代码审阅等等. 瀑布式 ...

  7. sift特征源码

    先贴上我对Opencv3.1中sift源码的注释吧,虽然还有很多没看懂.先从detectAndCompute看起 void SIFT_Impl::detectAndCompute(InputArray ...

  8. JavaScript------获取url地址中的参数

    $(document).ready(function () { //获取地址中的参数(name是字符串) function getParameter(name) { //正则表达式 var reg = ...

  9. dos 命令帮助文档chm

    http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx?mfr=true

  10. thinkphp 3.2 join

    $res2 = M('stat_info a') ->join(C('DB_PREFIX').'stock b ON a.goods_id = b.goods_id') ->field(' ...