使用WebClient或HttpWebRequest模拟上传文件和数据
2007-11-21 13:16
811 查看
假如某网站有个表单,例如(url: http://localhost/login.aspx):
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
帐号
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
密码
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
我们需要在程序中提交数据到这个表单,对于这种表单,我们可以使用 WebClient.UploadData 方法来实现,将所要上传的数据拼成字符即可,程序很简单:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
string uriString = "http://localhost/login.aspx";
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 创建一个新的 WebClient 实例.
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
WebClient myWebClient = new WebClient();
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
string postData = "Username=admin&Password=admin";
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 注意这种拼字符串的ContentType
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 转化成二进制数组
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 上传数据,并获取返回的二进制数据.
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray);
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
对于文件上传类的表单,例如(url: http://localhost/uploadFile.aspx):
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
文件
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
对于这种表单,我们可以使用
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
String uriString = "http://localhost/uploadFile.aspx";
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 创建一个新的 WebClient 实例.
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
WebClient myWebClient = new WebClient();
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
string fileName = @"C:/upload.txt";
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 直接上传,并获取返回的二进制数据.
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName);
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
还有一种表单,不仅有文字,还有文件,例如(url: http://localhost/uploadData.aspx):
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
文件名
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
文件
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
对于这种表单,似乎前面的两种方法都不能适用,对于第一种方法,不能直接拼字符串,对于第二种,我们只能传文件,重新回到第一个方法,注意参数:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public byte[] UploadData(
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
string address,
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
string method,
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
byte[] data
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
);
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在第一个例子中,是通过拼字符串来得到byte[] data参数值的,对于这种表单显然不行,反过来想想,对于uploadData.aspx这样的程序来说,直接通过网页提交数据,后台所获取到的流是什么样的呢?,最终的数据如下:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-----------------------------7d429871607fe
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Content-Disposition: form-data; name="file1"; filename="G:/homepage.txt"
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Content-Type: text/plain
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
宝玉:http://www.webuc.net
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-----------------------------7d429871607fe
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Content-Disposition: form-data; name="filename"
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
default filename
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-----------------------------7d429871607fe--
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
所以只要拼一个这样的byte[] data数据Post过去,就可以达到同样的效果了。但是一定要注意,对于这种带有文件上传的,其ContentType是不一样的,例如上面的这种,其ContentType为"multipart/form-data; boundary=---------------------------7d429871607fe"。有了ContentType,我们就可以知道boundary(就是上面的"---------------------------7d429871607fe"),知道boundary了我们就可以构造出我们所需要的byte[] data了,最后,不要忘记,把我们构造的ContentType传到WebClient中(例如:webClient.Headers.Add("Content-Type", ContentType);)这样,就可以通过WebClient.UploadData 方法上载文件数据了。
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
具体代码如下:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
生成二进制数据类的封装
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.IO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Net;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace UploadData.Common
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public class CreateBytes
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Encoding encoding = Encoding.UTF8;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] JoinBytes(ArrayList byteArrays)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int length = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int readLength = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 加上结束边界
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string endBoundary = Boundary + "-- ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] endBoundaryBytes = encoding.GetBytes(endBoundary);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byteArrays.Add(endBoundaryBytes);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach (byte[] b in byteArrays)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
length += b.Length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] bytes = new byte[length];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 遍历复制
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach (byte[] b in byteArrays)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
b.CopyTo(bytes, readLength);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
readLength += b.Length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return bytes;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public bool UploadData(string uploadUrl, byte[] bytes, out byte[] responseBytes)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WebClient webClient = new WebClient();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
webClient.Headers.Add("Content-Type", ContentType);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
responseBytes = webClient.UploadData(uploadUrl, bytes);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch (WebException ex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Stream resp = ex.Response.GetResponseStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
responseBytes = new byte[ex.Response.ContentLength];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
resp.Read(responseBytes, 0, responseBytes.Length);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// 获取普通表单区域二进制数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] CreateFieldData(string fieldName, string fieldValue)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string textTemplate = Boundary + " Content-Disposition: form-data; name="{0}" {1} ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string text = String.Format(textTemplate, fieldName, fieldValue);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] bytes = encoding.GetBytes(text);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return bytes;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] CreateFieldData(string fieldName, string filename, string contentType, byte[] fileBytes)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string end = " ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string textTemplate = Boundary + " Content-Disposition: form-data; name="{0}"; filename="{1}" Content-Type: {2} ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 头数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string data = String.Format(textTemplate, fieldName, filename, contentType);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] bytes = encoding.GetBytes(data);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 尾数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] endBytes = encoding.GetBytes(end);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 合成后的数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] fieldData = new byte[bytes.Length + fileBytes.Length + endBytes.Length];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bytes.CopyTo(fieldData, 0); // 头数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fileBytes.CopyTo(fieldData, bytes.Length); // 文件的二进制数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
endBytes.CopyTo(fieldData, bytes.Length + fileBytes.Length); //
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return fieldData;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string Boundary
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string[] bArray, ctArray;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string contentType = ContentType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ctArray = contentType.Split(';');
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (ctArray[0].Trim().ToLower() == "multipart/form-data")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bArray = ctArray[1].Split('=');
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return "--" + bArray[1];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string ContentType
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (HttpContext.Current == null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return "multipart/form-data; boundary=---------------------------7d5b915500cee";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return HttpContext.Current.Request.ContentType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Drawing;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.ComponentModel;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Windows.Forms;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using UploadData.Common;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.IO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace UploadDataWin
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public class frmUpload : System.Windows.Forms.Form
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.Label lblAmigoToken;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.TextBox txtAmigoToken;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.Label lblFilename;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.TextBox txtFilename;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.Button btnBrowse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.TextBox txtFileData;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.Label lblFileData;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.Button btnUpload;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.OpenFileDialog openFileDialog1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.TextBox txtResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.ComponentModel.Container components = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public frmUpload()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InitializeComponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected override void Dispose(bool disposing)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (disposing)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (components != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
components.Dispose();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
base.Dispose(disposing);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void InitializeComponent()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblAmigoToken = new System.Windows.Forms.Label();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtAmigoToken = new System.Windows.Forms.TextBox();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFilename = new System.Windows.Forms.Label();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFilename = new System.Windows.Forms.TextBox();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnBrowse = new System.Windows.Forms.Button();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFileData = new System.Windows.Forms.TextBox();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFileData = new System.Windows.Forms.Label();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnUpload = new System.Windows.Forms.Button();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtResponse = new System.Windows.Forms.TextBox();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.SuspendLayout();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// lblAmigoToken
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblAmigoToken.Location = new System.Drawing.Point(40, 48);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblAmigoToken.Name = "lblAmigoToken";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblAmigoToken.Size = new System.Drawing.Size(72, 23);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblAmigoToken.TabIndex = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblAmigoToken.Text = "AmigoToken";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// txtAmigoToken
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtAmigoToken.Location = new System.Drawing.Point(120, 48);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtAmigoToken.Name = "txtAmigoToken";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtAmigoToken.Size = new System.Drawing.Size(248, 21);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtAmigoToken.TabIndex = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtAmigoToken.Text = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// lblFilename
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFilename.Location = new System.Drawing.Point(40, 96);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFilename.Name = "lblFilename";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFilename.Size = new System.Drawing.Size(80, 23);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFilename.TabIndex = 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFilename.Text = "Filename";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// txtFilename
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFilename.Location = new System.Drawing.Point(120, 96);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFilename.Name = "txtFilename";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFilename.Size = new System.Drawing.Size(248, 21);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFilename.TabIndex = 3;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFilename.Text = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// btnBrowse
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnBrowse.Location = new System.Drawing.Point(296, 144);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnBrowse.Name = "btnBrowse";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnBrowse.TabIndex = 4;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnBrowse.Text = "浏览";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// txtFileData
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFileData.Location = new System.Drawing.Point(120, 144);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFileData.Name = "txtFileData";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFileData.Size = new System.Drawing.Size(168, 21);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFileData.TabIndex = 5;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFileData.Text = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// lblFileData
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFileData.Location = new System.Drawing.Point(40, 144);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFileData.Name = "lblFileData";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFileData.Size = new System.Drawing.Size(72, 23);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFileData.TabIndex = 6;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFileData.Text = "FileData";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// btnUpload
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnUpload.Location = new System.Drawing.Point(48, 184);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnUpload.Name = "btnUpload";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnUpload.TabIndex = 7;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnUpload.Text = "Upload";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// txtResponse
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtResponse.Location = new System.Drawing.Point(136, 184);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtResponse.Multiline = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtResponse.Name = "txtResponse";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtResponse.Size = new System.Drawing.Size(248, 72);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtResponse.TabIndex = 8;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtResponse.Text = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// frmUpload
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.ClientSize = new System.Drawing.Size(400, 269);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.txtResponse);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.btnUpload);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.lblFileData);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.txtFileData);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.btnBrowse);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.txtFilename);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.lblFilename);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.txtAmigoToken);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.lblAmigoToken);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Name = "frmUpload";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Text = "frmUpload";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.ResumeLayout(false);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[STAThread]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static void Main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.Run(new frmUpload());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void btnUpload_Click(object sender, System.EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 非空检验
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (txtAmigoToken.Text.Trim() == "" || txtFilename.Text == "" || txtFileData.Text.Trim() == "")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show("Please fill data");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 所要上传的文件路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string path = txtFileData.Text.Trim();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 检查文件是否存在
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!File.Exists(path))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show("{0} does not exist!", path);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 读文件流
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileStream fs = new FileStream(path, FileMode.Open,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileAccess.Read, FileShare.Read);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 这部分需要完善
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string ContentType = "application/octet-stream";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] fileBytes = new byte[fs.Length];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fs.Read(fileBytes, 0, Convert.ToInt32(fs.Length));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 生成需要上传的二进制数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CreateBytes cb = new CreateBytes();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 所有表单数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ArrayList bytesArray = new ArrayList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 普通表单
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bytesArray.Add(cb.CreateFieldData("FileName", txtFilename.Text));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bytesArray.Add(cb.CreateFieldData("AmigoToken", txtAmigoToken.Text));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 文件表单
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bytesArray.Add(cb.CreateFieldData("FileData", path
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
, ContentType, fileBytes));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 合成所有表单并生成二进制数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] bytes = cb.JoinBytes(bytesArray);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 返回的内容
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] responseBytes;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 上传到指定Url
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bool uploaded = cb.UploadData("http://localhost/UploadData/UploadAvatar.aspx", bytes, out responseBytes);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 将返回的内容输出到文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
using (FileStream file = new FileStream(@"c: esponse.text", FileMode.Create, FileAccess.Write, FileShare.Read))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
file.Write(responseBytes, 0, responseBytes.Length);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
txtResponse.Text = System.Text.Encoding.UTF8.GetString(responseBytes);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void btnBrowse_Click(object sender, System.EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (openFileDialog1.ShowDialog() == DialogResult.OK)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
txtFileData.Text = openFileDialog1.FileName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
帐号
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
密码
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
我们需要在程序中提交数据到这个表单,对于这种表单,我们可以使用 WebClient.UploadData 方法来实现,将所要上传的数据拼成字符即可,程序很简单:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
string uriString = "http://localhost/login.aspx";
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 创建一个新的 WebClient 实例.
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
WebClient myWebClient = new WebClient();
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
string postData = "Username=admin&Password=admin";
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 注意这种拼字符串的ContentType
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 转化成二进制数组
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 上传数据,并获取返回的二进制数据.
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray);
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
对于文件上传类的表单,例如(url: http://localhost/uploadFile.aspx):
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
文件
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
对于这种表单,我们可以使用
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
String uriString = "http://localhost/uploadFile.aspx";
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 创建一个新的 WebClient 实例.
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
WebClient myWebClient = new WebClient();
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
string fileName = @"C:/upload.txt";
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 直接上传,并获取返回的二进制数据.
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName);
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
还有一种表单,不仅有文字,还有文件,例如(url: http://localhost/uploadData.aspx):
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
文件名
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
文件
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
对于这种表单,似乎前面的两种方法都不能适用,对于第一种方法,不能直接拼字符串,对于第二种,我们只能传文件,重新回到第一个方法,注意参数:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public byte[] UploadData(
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
string address,
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
string method,
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
byte[] data
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
);
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在第一个例子中,是通过拼字符串来得到byte[] data参数值的,对于这种表单显然不行,反过来想想,对于uploadData.aspx这样的程序来说,直接通过网页提交数据,后台所获取到的流是什么样的呢?,最终的数据如下:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-----------------------------7d429871607fe
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Content-Disposition: form-data; name="file1"; filename="G:/homepage.txt"
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Content-Type: text/plain
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
宝玉:http://www.webuc.net
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-----------------------------7d429871607fe
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Content-Disposition: form-data; name="filename"
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
default filename
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-----------------------------7d429871607fe--
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
所以只要拼一个这样的byte[] data数据Post过去,就可以达到同样的效果了。但是一定要注意,对于这种带有文件上传的,其ContentType是不一样的,例如上面的这种,其ContentType为"multipart/form-data; boundary=---------------------------7d429871607fe"。有了ContentType,我们就可以知道boundary(就是上面的"---------------------------7d429871607fe"),知道boundary了我们就可以构造出我们所需要的byte[] data了,最后,不要忘记,把我们构造的ContentType传到WebClient中(例如:webClient.Headers.Add("Content-Type", ContentType);)这样,就可以通过WebClient.UploadData 方法上载文件数据了。
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
具体代码如下:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
生成二进制数据类的封装
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.IO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Net;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace UploadData.Common
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public class CreateBytes
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Encoding encoding = Encoding.UTF8;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] JoinBytes(ArrayList byteArrays)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int length = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int readLength = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 加上结束边界
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string endBoundary = Boundary + "-- ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] endBoundaryBytes = encoding.GetBytes(endBoundary);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byteArrays.Add(endBoundaryBytes);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach (byte[] b in byteArrays)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
length += b.Length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] bytes = new byte[length];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 遍历复制
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach (byte[] b in byteArrays)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
b.CopyTo(bytes, readLength);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
readLength += b.Length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return bytes;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public bool UploadData(string uploadUrl, byte[] bytes, out byte[] responseBytes)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WebClient webClient = new WebClient();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
webClient.Headers.Add("Content-Type", ContentType);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
responseBytes = webClient.UploadData(uploadUrl, bytes);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch (WebException ex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Stream resp = ex.Response.GetResponseStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
responseBytes = new byte[ex.Response.ContentLength];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
resp.Read(responseBytes, 0, responseBytes.Length);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// 获取普通表单区域二进制数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] CreateFieldData(string fieldName, string fieldValue)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string textTemplate = Boundary + " Content-Disposition: form-data; name="{0}" {1} ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string text = String.Format(textTemplate, fieldName, fieldValue);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] bytes = encoding.GetBytes(text);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return bytes;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] CreateFieldData(string fieldName, string filename, string contentType, byte[] fileBytes)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string end = " ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string textTemplate = Boundary + " Content-Disposition: form-data; name="{0}"; filename="{1}" Content-Type: {2} ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 头数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string data = String.Format(textTemplate, fieldName, filename, contentType);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] bytes = encoding.GetBytes(data);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 尾数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] endBytes = encoding.GetBytes(end);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 合成后的数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] fieldData = new byte[bytes.Length + fileBytes.Length + endBytes.Length];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bytes.CopyTo(fieldData, 0); // 头数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fileBytes.CopyTo(fieldData, bytes.Length); // 文件的二进制数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
endBytes.CopyTo(fieldData, bytes.Length + fileBytes.Length); //
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return fieldData;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string Boundary
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string[] bArray, ctArray;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string contentType = ContentType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ctArray = contentType.Split(';');
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (ctArray[0].Trim().ToLower() == "multipart/form-data")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bArray = ctArray[1].Split('=');
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return "--" + bArray[1];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string ContentType
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (HttpContext.Current == null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return "multipart/form-data; boundary=---------------------------7d5b915500cee";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return HttpContext.Current.Request.ContentType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Drawing;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.ComponentModel;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Windows.Forms;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using UploadData.Common;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.IO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace UploadDataWin
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public class frmUpload : System.Windows.Forms.Form
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.Label lblAmigoToken;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.TextBox txtAmigoToken;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.Label lblFilename;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.TextBox txtFilename;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.Button btnBrowse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.TextBox txtFileData;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.Label lblFileData;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.Button btnUpload;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.OpenFileDialog openFileDialog1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.TextBox txtResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private System.ComponentModel.Container components = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public frmUpload()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InitializeComponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected override void Dispose(bool disposing)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (disposing)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (components != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
components.Dispose();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
base.Dispose(disposing);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void InitializeComponent()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblAmigoToken = new System.Windows.Forms.Label();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtAmigoToken = new System.Windows.Forms.TextBox();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFilename = new System.Windows.Forms.Label();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFilename = new System.Windows.Forms.TextBox();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnBrowse = new System.Windows.Forms.Button();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFileData = new System.Windows.Forms.TextBox();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFileData = new System.Windows.Forms.Label();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnUpload = new System.Windows.Forms.Button();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtResponse = new System.Windows.Forms.TextBox();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.SuspendLayout();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// lblAmigoToken
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblAmigoToken.Location = new System.Drawing.Point(40, 48);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblAmigoToken.Name = "lblAmigoToken";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblAmigoToken.Size = new System.Drawing.Size(72, 23);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblAmigoToken.TabIndex = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblAmigoToken.Text = "AmigoToken";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// txtAmigoToken
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtAmigoToken.Location = new System.Drawing.Point(120, 48);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtAmigoToken.Name = "txtAmigoToken";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtAmigoToken.Size = new System.Drawing.Size(248, 21);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtAmigoToken.TabIndex = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtAmigoToken.Text = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// lblFilename
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFilename.Location = new System.Drawing.Point(40, 96);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFilename.Name = "lblFilename";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFilename.Size = new System.Drawing.Size(80, 23);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFilename.TabIndex = 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFilename.Text = "Filename";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// txtFilename
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFilename.Location = new System.Drawing.Point(120, 96);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFilename.Name = "txtFilename";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFilename.Size = new System.Drawing.Size(248, 21);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFilename.TabIndex = 3;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFilename.Text = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// btnBrowse
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnBrowse.Location = new System.Drawing.Point(296, 144);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnBrowse.Name = "btnBrowse";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnBrowse.TabIndex = 4;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnBrowse.Text = "浏览";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// txtFileData
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFileData.Location = new System.Drawing.Point(120, 144);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFileData.Name = "txtFileData";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFileData.Size = new System.Drawing.Size(168, 21);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFileData.TabIndex = 5;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtFileData.Text = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// lblFileData
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFileData.Location = new System.Drawing.Point(40, 144);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFileData.Name = "lblFileData";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFileData.Size = new System.Drawing.Size(72, 23);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFileData.TabIndex = 6;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.lblFileData.Text = "FileData";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// btnUpload
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnUpload.Location = new System.Drawing.Point(48, 184);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnUpload.Name = "btnUpload";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnUpload.TabIndex = 7;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnUpload.Text = "Upload";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// txtResponse
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtResponse.Location = new System.Drawing.Point(136, 184);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtResponse.Multiline = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtResponse.Name = "txtResponse";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtResponse.Size = new System.Drawing.Size(248, 72);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtResponse.TabIndex = 8;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.txtResponse.Text = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// frmUpload
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.ClientSize = new System.Drawing.Size(400, 269);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.txtResponse);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.btnUpload);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.lblFileData);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.txtFileData);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.btnBrowse);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.txtFilename);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.lblFilename);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.txtAmigoToken);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(this.lblAmigoToken);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Name = "frmUpload";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Text = "frmUpload";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.ResumeLayout(false);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[STAThread]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static void Main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.Run(new frmUpload());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void btnUpload_Click(object sender, System.EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 非空检验
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (txtAmigoToken.Text.Trim() == "" || txtFilename.Text == "" || txtFileData.Text.Trim() == "")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show("Please fill data");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 所要上传的文件路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string path = txtFileData.Text.Trim();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 检查文件是否存在
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!File.Exists(path))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show("{0} does not exist!", path);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 读文件流
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileStream fs = new FileStream(path, FileMode.Open,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileAccess.Read, FileShare.Read);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 这部分需要完善
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string ContentType = "application/octet-stream";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] fileBytes = new byte[fs.Length];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fs.Read(fileBytes, 0, Convert.ToInt32(fs.Length));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 生成需要上传的二进制数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CreateBytes cb = new CreateBytes();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 所有表单数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ArrayList bytesArray = new ArrayList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 普通表单
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bytesArray.Add(cb.CreateFieldData("FileName", txtFilename.Text));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bytesArray.Add(cb.CreateFieldData("AmigoToken", txtAmigoToken.Text));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 文件表单
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bytesArray.Add(cb.CreateFieldData("FileData", path
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
, ContentType, fileBytes));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 合成所有表单并生成二进制数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] bytes = cb.JoinBytes(bytesArray);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 返回的内容
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] responseBytes;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 上传到指定Url
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bool uploaded = cb.UploadData("http://localhost/UploadData/UploadAvatar.aspx", bytes, out responseBytes);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 将返回的内容输出到文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
using (FileStream file = new FileStream(@"c: esponse.text", FileMode.Create, FileAccess.Write, FileShare.Read))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
file.Write(responseBytes, 0, responseBytes.Length);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
txtResponse.Text = System.Text.Encoding.UTF8.GetString(responseBytes);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void btnBrowse_Click(object sender, System.EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (openFileDialog1.ShowDialog() == DialogResult.OK)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
txtFileData.Text = openFileDialog1.FileName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- 使用WebClient或HttpWebRequest模拟上传文件和数据
- 使用WebClient或HttpWebRequest模拟上传文件和数据
- 使用WebClient或HttpWebRequest模拟上传文件和数据
- 使用WebClient或HttpWebRequest模拟上传文件和数据
- 使用HttpWebRequest向网站模拟上传数据-(摘自greystar的专栏)
- 使用HttpWebRequest向网站模拟上传数据
- 使用HttpWebRequest向网站模拟上传数据
- 使用HttpWebRequest向网站模拟上传数据
- 使用HttpWebRequest向网站模拟上传数据
- httpClient4.3 模拟Post提交,模拟Post上传文件并解决服务端使用spingMVC时upload.parseRequest(request)解析不到File,Pos简单抓取页面数据
- asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- 使用HttpWebRequest向网站模拟上传数据
- 使用HttpWebRequest向网站模拟上传数据
- 文件上传 content-type:application/json System.Web.HttpContext.Current.Request.InputStream 读取不到数据
- [C#]在WinForm下使用HttpWebRequest上传文件并显示进度
- HttpWebRequest模拟上传文件
- C# 使用HttpWebRequest 实现文件的上传
- C#在WinForm下使用HttpWebRequest上传文件并显示进度
- [转]C#在WinForm下使用HttpWebRequest上传文件并显示进度