WebClient 实现多文件/文本同时上传
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 实现多文件/文本同时上传的更多相关文章
- 文件无刷新上传(swfUpload与uploadify)
文件无刷新上传并获取保存到服务器端的路径 遇到上传文件的问题,结合之前用到过的swfUpload,又找了一个无刷新上传文件的jquery插件uploadify,写篇博客记录一下分别介绍这两个插件的实现 ...
- html5 文件拖拽上传
本文首先发表在 码蜂笔记 : http://coderbee.net/index.php/web/20130703/266 html5 文件拖拽上传是个老话题了,网上有很多例子,我一开始的代码也是网 ...
- 【Android实战】----基于Retrofit实现多图片/文件、图文上传
本文代码详见:https://github.com/honghailiang/RetrofitUpLoadImage 一.再次膜拜下Retrofit Retrofit不管从性能还是使用方便性上都非常屌 ...
- Java web开发——文件夹的上传和下载
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...
- 需求-java web 能够实现整个文件夹的上传下载吗?
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...
- Java实现FTP文件与文件夹的上传和下载
Java实现FTP文件与文件夹的上传和下载 FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文传协议".用于Internet上的控制 ...
- [New Portal]Windows Azure Virtual Machine (15) 在本地制作数据文件VHD并上传至Azure(2)
<Windows Azure Platform 系列文章目录> 在上一章内容里,我们已经将包含有OFFICE2013 ISO安装文件的VHD上传至Azure Blob Storage中了. ...
- WEB版一次选择多个文件进行批量上传(Plupload)的解决方案
WEB版一次选择多个文件进行批量上传(Plupload)的解决方案 转载自http://www.cnblogs.com/chillsrc/archive/2013/01/30/2883648.htm ...
- apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出
用到了apache 的 commons-net-3.0.1.jar 和 log4j-1.2.15.jar 这连个jar包 JAVA 代码如下: package com.bjut.edu.cn.ftp; ...
随机推荐
- jBPM4.4 no jBPM DB schema: no JBPM4_EXECUTION table. Run the create.jbpm.schema target first in the install tool.
jBPM4.4 no jBPM DB schema: no JBPM4_EXECUTION table. Run the create.jbpm.schema target first in the ...
- iOS 解决LaunchScreen中图片加载黑屏问题
iOS 解决LaunchScreen中图片加载黑屏问题 原文: http://blog.csdn.net/chengkaizone/article/details/50478045 iOS 解决Lau ...
- [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
- 怎样简单灵活地将DataTable中的数据赋值给model
最近在做的一个项目中,有13个方法都需要用到同一种处理方式:通过SQL语句从数据库获取一条指定的数据,并将该数据中的每个值都赋值给一个model,再将这个model中的数据通过微信发送出去.每个方法都 ...
- JavaScript模板引擎artTemplate.js——结语
再次首先感谢模板的作者大神,再次放出github的地址:artTemplate性能卓越的js模板引擎 然后感谢博客园的一位前辈,他写的handlebars.js模板引擎教程,对我提供了很大的帮助,也是 ...
- 用vue.js学习es6(五):set和map的使用
一:Set用法: ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. (1).打印:console.log var data = new Set([1,2,3]); ...
- php杂项
php5.3新增闭包函数用法use用法(引入变量地址且随内存中值变化而变化,跳过解析顺序直接获取函数最终值) $obj = (object) "Hello, everyone"; ...
- 如何有效地解决ie7,IE8不支持document.getElmentsByClassName的问题
1.复制此代码到你js代码的最前面即可 if(!document.getElementsByClassName){ document.getElementsByClassName = functi ...
- 【UOJ #35】后缀排序 后缀数组模板
http://uoj.ac/problem/35 以前做后缀数组的题直接粘模板...现在重新写一下模板 注意用来基数排序的数组一定要开到N. #include<cstdio> #inclu ...
- apche启动错误|httpd.pid overwritten — Unclean shutdown of previous Apache run?
APACHE启动成功,但无法接受任何请求,查看ERROR.LOG文件[warn] pid file /opt/apache/logs/httpd.pid overwritten - Unclean s ...