使用WebClient或HttpWebRequest模拟上传文件和数据

None.gif 假如某网站有个表单,例如(url: http: // localhost/login.aspx):
None.gif
帐号  
None.gif密码  
None.gif
None.gif我们需要在程序中提交数据到这个表单,对于这种表单,我们可以使用 WebClient.UploadData 方法来实现,将所要上传的数据拼成字符即可,程序很简单:
None.gif
None.gif
string  uriString  =   " http://localhost/login.aspx " ;
None.gif
//  创建一个新的 WebClient 实例.
None.gif
WebClient myWebClient  =   new  WebClient();
None.gif
string  postData  =   " Username=admin&Password=admin " ;
None.gif
//  注意这种拼字符串的ContentType
None.gif
myWebClient.Headers.Add( " Content-Type " , " application/x-www-form-urlencoded " );
None.gif
//  转化成二进制数组
None.gif
byte [] byteArray  =  Encoding.ASCII.GetBytes(postData);
None.gif
//  上传数据,并获取返回的二进制数据.
None.gif
byte [] responseArray  =  myWebClient.UploadData(uriString, " POST " ,byteArray);
None.gif
None.gif
None.gif对于文件上传类的表单,例如(url: http:
// localhost/uploadFile.aspx):
None.gif
文件  
None.gif
None.gif对于这种表单,我们可以使用
None.gifString uriString 
=   " http://localhost/uploadFile.aspx " ;
None.gif
None.gif
//  创建一个新的 WebClient 实例.
None.gif
WebClient myWebClient  =   new  WebClient();
None.gif
None.gif
string  fileName  =   @" C:\upload.txt " ;
None.gif
None.gif
//  直接上传,并获取返回的二进制数据.
None.gif
byte [] responseArray  =  myWebClient.UploadFile(uriString, " POST " ,fileName);
None.gif
None.gif
None.gif还有一种表单,不仅有文字,还有文件,例如(url: http:
// localhost/uploadData.aspx):
None.gif
文件名  
None.gif文件  
None.gif
None.gif对于这种表单,似乎前面的两种方法都不能适用,对于第一种方法,不能直接拼字符串,对于第二种,我们只能传文件,重新回到第一个方法,注意参数:
None.gif
public   byte [] UploadData(
None.gif   
string  address,
None.gif   
string  method,
None.gif   
byte [] data
None.gif);
None.gif在第一个例子中,是通过拼字符串来得到byte[] data参数值的,对于这种表单显然不行,反过来想想,对于uploadData.aspx这样的程序来说,直接通过网页提交数据,后台所获取到的流是什么样的呢?(在我以前的一篇blog中,曾分析过这个问题:asp无组件上传进度条解决方案),最终的数据如下:
None.gif
None.gif
----------------------------- 7d429871607fe
None.gifContent
- Disposition: form - data; name = " file1 " ; filename = " G:\homepage.txt "
None.gifContent
- Type: text / plain
None.gif宝玉:http:
// www.webuc.net
None.gif
----------------------------- 7d429871607fe
None.gifContent
- Disposition: form - data; name = " filename "
None.gif
default  filename
None.gif
----------------------------- 7d429871607fe --
None.gif
None.gif
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 方法上载文件数据了。
None.gif
None.gif具体代码如下:
None.gif生成二进制数据类的封装
None.gif
None.gif
using  System;
None.gif
using  System.Web;
None.gif
using  System.IO;
None.gif
using  System.Net;
None.gif
using  System.Text;
None.gif
using  System.Collections;
None.gif
None.gif
namespace  UploadData.Common
ExpandedBlockStart.gifContractedBlock.gifdot.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**//**//// <summary>
InBlock.gif    
/// 创建WebClient.UploadData方法所需二进制数组
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class CreateBytes
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
dot.gif{
InBlock.gif        Encoding encoding 
= Encoding.UTF8;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 拼接所有的二进制数组为一个数组
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="byteArrays">数组</param>
InBlock.gif        
/// <returns></returns>
ExpandedSubBlockEnd.gif        
/// <remarks>加上结束边界</remarks>

InBlock.gif        public byte[] JoinBytes(ArrayList byteArrays)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            
int length = 0;
InBlock.gif            
int readLength = 0;
InBlock.gif
InBlock.gif            
// 加上结束边界
InBlock.gif
            string endBoundary = Boundary + "--\r\n"//结束边界
InBlock.gif
            byte[] endBoundaryBytes = encoding.GetBytes(endBoundary);
InBlock.gif            byteArrays.Add(endBoundaryBytes);
InBlock.gif
InBlock.gif            
foreach(byte[] b in byteArrays)
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                length 
+= b.Length;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
byte[] bytes = new byte[length];
InBlock.gif
InBlock.gif            
// 遍历复制
InBlock.gif            
//
InBlock.gif
            foreach(byte[] b in byteArrays)
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                b.CopyTo(bytes, readLength);
InBlock.gif                readLength 
+= b.Length;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return bytes;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool UploadData(string uploadUrl, byte[] bytes, out byte[] responseBytes)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            WebClient webClient 
= new WebClient();
InBlock.gif            webClient.Headers.Add(
"Content-Type", ContentType);
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                responseBytes 
= webClient.UploadData(uploadUrl, bytes);
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (WebException ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                Stream resp 
= ex.Response.GetResponseStream();
InBlock.gif                responseBytes 
= new byte[ex.Response.ContentLength];
InBlock.gif                resp.Read(responseBytes, 
0, responseBytes.Length);                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return false
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 获取普通表单区域二进制数组
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="fieldName">表单名</param>
InBlock.gif        
/// <param name="fieldValue">表单值</param>
InBlock.gif        
/// <returns></returns>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// -----------------------------7d52ee27210a3c\r\nContent-Disposition: form-data; name=\"表单名\"\r\n\r\n表单值\r\n
ExpandedSubBlockEnd.gif        
/// </remarks>

InBlock.gif        public byte[] CreateFieldData(string fieldName, string fieldValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string textTemplate = Boundary + "\r\nContent-Disposition: form-data; name=\"dot.gif{0}\"\r\n\r\n{1}\r\n";
InBlock.gif            
string text = String.Format(textTemplate, fieldName, fieldValue);
InBlock.gif            
byte[] bytes = encoding.GetBytes(text);
InBlock.gif            
return bytes;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 获取文件上传表单区域二进制数组
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="fieldName">表单名</param>
InBlock.gif        
/// <param name="filename">文件名</param>
InBlock.gif        
/// <param name="contentType">文件类型</param>
InBlock.gif        
/// <param name="contentLength">文件长度</param>
InBlock.gif        
/// <param name="stream">文件流</param>
ExpandedSubBlockEnd.gif        
/// <returns>二进制数组</returns>

InBlock.gif        public byte[] CreateFieldData(string fieldName, string filename,string contentType, byte[] fileBytes)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            
string end = "\r\n";
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string textTemplate = Boundary + "\r\nContent-Disposition: form-data; name=\"dot.gif{0}\"; filename=\"dot.gif{1}\"\r\nContent-Type: {2}\r\n\r\n";
InBlock.gif            
InBlock.gif            
// 头数据
InBlock.gif
            string data = String.Format(textTemplate, fieldName, filename, contentType);
InBlock.gif            
byte[] bytes = encoding.GetBytes(data);
InBlock.gif
InBlock.gif            
InBlock.gif
InBlock.gif            
// 尾数据
InBlock.gif
            byte[] endBytes = encoding.GetBytes(end);
InBlock.gif
InBlock.gif            
// 合成后的数组
InBlock.gif
            byte[] fieldData = new byte[bytes.Length + fileBytes.Length + endBytes.Length];
InBlock.gif
InBlock.gif            bytes.CopyTo(fieldData, 
0); // 头数据
InBlock.gif
            fileBytes.CopyTo(fieldData, bytes.Length); // 文件的二进制数据
InBlock.gif
            endBytes.CopyTo(fieldData, bytes.Length + fileBytes.Length); // \r\n
InBlock.gif

InBlock.gif            
return fieldData;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        属性
属性#region 属性
InBlock.gif        
public string Boundary
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                
string[] bArray, ctArray;
InBlock.gif                
string contentType = ContentType;
InBlock.gif                ctArray 
= contentType.Split(';');
InBlock.gif                
if (ctArray[0].Trim().ToLower() == "multipart/form-data")
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif
dot.gif{
InBlock.gif                    bArray 
= ctArray[1].Split('=');
InBlock.gif                    
return "--" + bArray[1];
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string ContentType
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifdot.gif{
InBlock.gif                
if (HttpContext.Current == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif
dot.gif{
InBlock.gif                    
return "multipart/form-data; boundary=---------------------------7d5b915500cee";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return HttpContext.Current.Request.ContentType;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif在Winform中调用
None.gif
None.gif
None.gif
using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Data;
None.gif
None.gif
using  UploadData.Common;
None.gif
using  System.IO;
None.gif
None.gif
namespace  UploadDataWin
ExpandedBlockStart.gifContractedBlock.gifdot.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**//**//// <summary>
InBlock.gif    
/// frmUpload 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class frmUpload : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
dot.gif{
InBlock.gif        
private System.Windows.Forms.Label lblAmigoToken;
InBlock.gif        
private System.Windows.Forms.TextBox txtAmigoToken;
InBlock.gif        
private System.Windows.Forms.Label lblFilename;
InBlock.gif        
private System.Windows.Forms.TextBox txtFilename;
InBlock.gif        
private System.Windows.Forms.Button btnBrowse;
InBlock.gif        
private System.Windows.Forms.TextBox txtFileData;
InBlock.gif        
private System.Windows.Forms.Label lblFileData;
InBlock.gif        
private System.Windows.Forms.Button btnUpload;
InBlock.gif        
private System.Windows.Forms.OpenFileDialog openFileDialog1;
InBlock.gif        
private System.Windows.Forms.TextBox txtResponse;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public frmUpload()
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            
//
InBlock.gif            
// Windows 窗体设计器支持所必需的
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
//
InBlock.gif            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        Windows 窗体设计器生成的代码
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            
this.lblAmigoToken = new System.Windows.Forms.Label();
InBlock.gif            
this.txtAmigoToken = new System.Windows.Forms.TextBox();
InBlock.gif            
this.lblFilename = new System.Windows.Forms.Label();
InBlock.gif            
this.txtFilename = new System.Windows.Forms.TextBox();
InBlock.gif            
this.btnBrowse = new System.Windows.Forms.Button();
InBlock.gif            
this.txtFileData = new System.Windows.Forms.TextBox();
InBlock.gif            
this.lblFileData = new System.Windows.Forms.Label();
InBlock.gif            
this.btnUpload = new System.Windows.Forms.Button();
InBlock.gif            
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
InBlock.gif            
this.txtResponse = new System.Windows.Forms.TextBox();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// lblAmigoToken
InBlock.gif            
// 
InBlock.gif
            this.lblAmigoToken.Location = new System.Drawing.Point(4048);
InBlock.gif            
this.lblAmigoToken.Name = "lblAmigoToken";
InBlock.gif            
this.lblAmigoToken.Size = new System.Drawing.Size(7223);
InBlock.gif            
this.lblAmigoToken.TabIndex = 0;
InBlock.gif            
this.lblAmigoToken.Text = "AmigoToken";
InBlock.gif            
// 
InBlock.gif            
// txtAmigoToken
InBlock.gif            
// 
InBlock.gif
            this.txtAmigoToken.Location = new System.Drawing.Point(12048);
InBlock.gif            
this.txtAmigoToken.Name = "txtAmigoToken";
InBlock.gif            
this.txtAmigoToken.Size = new System.Drawing.Size(24821);
InBlock.gif            
this.txtAmigoToken.TabIndex = 1;
InBlock.gif            
this.txtAmigoToken.Text = "";
InBlock.gif            
// 
InBlock.gif            
// lblFilename
InBlock.gif            
// 
InBlock.gif
            this.lblFilename.Location = new System.Drawing.Point(4096);
InBlock.gif            
this.lblFilename.Name = "lblFilename";
InBlock.gif            
this.lblFilename.Size = new System.Drawing.Size(8023);
InBlock.gif            
this.lblFilename.TabIndex = 2;
InBlock.gif            
this.lblFilename.Text = "Filename";
InBlock.gif            
// 
InBlock.gif            
// txtFilename
InBlock.gif            
// 
InBlock.gif
            this.txtFilename.Location = new System.Drawing.Point(12096);
InBlock.gif            
this.txtFilename.Name = "txtFilename";
InBlock.gif            
this.txtFilename.Size = new System.Drawing.Size(24821);
InBlock.gif            
this.txtFilename.TabIndex = 3;
InBlock.gif            
this.txtFilename.Text = "";
InBlock.gif            
// 
InBlock.gif            
// btnBrowse
InBlock.gif            
// 
InBlock.gif
            this.btnBrowse.Location = new System.Drawing.Point(296144);
InBlock.gif            
this.btnBrowse.Name = "btnBrowse";
InBlock.gif            
this.btnBrowse.TabIndex = 4;
InBlock.gif            
this.btnBrowse.Text = "浏览dot.gif";
InBlock.gif            
this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
InBlock.gif            
// 
InBlock.gif            
// txtFileData
InBlock.gif            
// 
InBlock.gif
            this.txtFileData.Location = new System.Drawing.Point(120144);
InBlock.gif            
this.txtFileData.Name = "txtFileData";
InBlock.gif            
this.txtFileData.Size = new System.Drawing.Size(16821);
InBlock.gif            
this.txtFileData.TabIndex = 5;
InBlock.gif            
this.txtFileData.Text = "";
InBlock.gif            
// 
InBlock.gif            
// lblFileData
InBlock.gif            
// 
InBlock.gif
            this.lblFileData.Location = new System.Drawing.Point(40144);
InBlock.gif            
this.lblFileData.Name = "lblFileData";
InBlock.gif            
this.lblFileData.Size = new System.Drawing.Size(7223);
InBlock.gif            
this.lblFileData.TabIndex = 6;
InBlock.gif            
this.lblFileData.Text = "FileData";
InBlock.gif            
// 
InBlock.gif            
// btnUpload
InBlock.gif            
// 
InBlock.gif
            this.btnUpload.Location = new System.Drawing.Point(48184);
InBlock.gif            
this.btnUpload.Name = "btnUpload";
InBlock.gif            
this.btnUpload.TabIndex = 7;
InBlock.gif            
this.btnUpload.Text = "Upload";
InBlock.gif            
this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
InBlock.gif            
// 
InBlock.gif            
// txtResponse
InBlock.gif            
// 
InBlock.gif
            this.txtResponse.Location = new System.Drawing.Point(136184);
InBlock.gif            
this.txtResponse.Multiline = true;
InBlock.gif            
this.txtResponse.Name = "txtResponse";
InBlock.gif            
this.txtResponse.Size = new System.Drawing.Size(24872);
InBlock.gif            
this.txtResponse.TabIndex = 8;
InBlock.gif            
this.txtResponse.Text = "";
InBlock.gif            
// 
InBlock.gif            
// frmUpload
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif            
this.ClientSize = new System.Drawing.Size(400269);
InBlock.gif            
this.Controls.Add(this.txtResponse);
InBlock.gif            
this.Controls.Add(this.btnUpload);
InBlock.gif            
this.Controls.Add(this.lblFileData);
InBlock.gif            
this.Controls.Add(this.txtFileData);
InBlock.gif            
this.Controls.Add(this.btnBrowse);
InBlock.gif            
this.Controls.Add(this.txtFilename);
InBlock.gif            
this.Controls.Add(this.lblFilename);
InBlock.gif            
this.Controls.Add(this.txtAmigoToken);
InBlock.gif            
this.Controls.Add(this.lblAmigoToken);
InBlock.gif            
this.Name = "frmUpload";
InBlock.gif            
this.Text = "frmUpload";
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            Application.Run(
new frmUpload());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void btnUpload_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            
// 非空检验
InBlock.gif
            if (txtAmigoToken.Text.Trim() == "" || txtFilename.Text == "" || txtFileData.Text.Trim() == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                MessageBox.Show(
"Please fill data");
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// 所要上传的文件路径
InBlock.gif
            string path = txtFileData.Text.Trim();
InBlock.gif
InBlock.gif            
// 检查文件是否存在
InBlock.gif
            if (!File.Exists(path)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                MessageBox.Show(
"{0} does not exist!", path);
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// 读文件流
InBlock.gif
            FileStream fs = new FileStream(path, FileMode.Open,
InBlock.gif                FileAccess.Read, FileShare.Read);
InBlock.gif            
InBlock.gif            
// 这部分需要完善
InBlock.gif
            string ContentType = "application/octet-stream";
InBlock.gif            
byte[] fileBytes = new byte[fs.Length];
InBlock.gif            fs.Read(fileBytes, 
0, Convert.ToInt32(fs.Length));
InBlock.gif
InBlock.gif
InBlock.gif            
// 生成需要上传的二进制数组
InBlock.gif
            CreateBytes cb = new CreateBytes();
InBlock.gif            
// 所有表单数据
InBlock.gif
            ArrayList bytesArray = new ArrayList();
InBlock.gif            
// 普通表单
InBlock.gif
            bytesArray.Add(cb.CreateFieldData("FileName", txtFilename.Text));
InBlock.gif            bytesArray.Add(cb.CreateFieldData(
"AmigoToken", txtAmigoToken.Text));
InBlock.gif            
// 文件表单
InBlock.gif
            bytesArray.Add(cb.CreateFieldData("FileData", path
InBlock.gif                                                , ContentType, fileBytes));
InBlock.gif
InBlock.gif            
// 合成所有表单并生成二进制数组
InBlock.gif
            byte[] bytes = cb.JoinBytes(bytesArray);
InBlock.gif            
InBlock.gif            
// 返回的内容
InBlock.gif
            byte[] responseBytes;
InBlock.gif            
InBlock.gif            
// 上传到指定Url
InBlock.gif
            bool uploaded = cb.UploadData("http://localhost/UploadData/UploadAvatar.aspx", bytes, out responseBytes);
InBlock.gif
InBlock.gif            
// 将返回的内容输出到文件
InBlock.gif
            using (FileStream file = new FileStream(@"c:\response.text", FileMode.Create, FileAccess.Write, FileShare.Read))
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                file.Write(responseBytes, 
0, responseBytes.Length);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            txtResponse.Text 
= System.Text.Encoding.UTF8.GetString(responseBytes);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void btnBrowse_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            
if(openFileDialog1.ShowDialog() == DialogResult.OK)
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                txtFileData.Text 
= openFileDialog1.FileName;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值