用 WebClient.UploadData 方法 上载文件数据
点击次数:21 次 发布日期:2008-11-27 00:00:29 作者:源代码网
|
源代码网推荐 帐号 源代码网推荐 密码 源代码网推荐 源代码网推荐 我们需要在程序中提交数据到这个表单,对于这种表单,我们可以使用 WebClient.UploadData 方法来实现,将所要上传的数据拼成字符即可,程序很简单: 源代码网推荐 源代码网推荐 string uriString = "http://localhost/login.aspx"; 源代码网推荐 // 创建一个新的 WebClient 实例. 源代码网推荐 WebClient myWebClient = new WebClient(); 源代码网推荐 string postData = "Username=admin&Password=admin"; 源代码网推荐 // 注意这种拼字符串的ContentType 源代码网推荐 myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded"); 源代码网推荐 // 转化成二进制数组 源代码网推荐 byte[] byteArray = Encoding.ASCII.GetBytes(postData); 源代码网推荐 // 上传数据,并获取返回的二进制数据. 源代码网推荐 byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray); 源代码网推荐 源代码网推荐 源代码网推荐 对于文件上传类的表单,例如(url: http://localhost/uploadFile.aspx): 源代码网推荐 文件 源代码网推荐 源代码网推荐 对于这种表单,我们可以使用 源代码网推荐 String uriString = "http://localhost/uploadFile.aspx"; 源代码网推荐 源代码网推荐 // 创建一个新的 WebClient 实例. 源代码网推荐 WebClient myWebClient = new WebClient(); 源代码网推荐 源代码网推荐 string fileName = @"C:upload.txt"; 源代码网推荐 源代码网推荐 // 直接上传,并获取返回的二进制数据. 源代码网推荐 byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName); 源代码网推荐 源代码网推荐 源代码网推荐 还有一种表单,不仅有文字,还有文件,例如(url: http://localhost/uploadData.aspx): 源代码网推荐 文件名 源代码网推荐 文件 源代码网推荐 源代码网推荐 对于这种表单,似乎前面的两种方法都不能适用,对于第一种方法,不能直接拼字符串,对于第二种,我们只能传文件,重新回到第一个方法,注意参数: 源代码网推荐 public byte[] UploadData( 源代码网推荐 string address, 源代码网推荐 string method, 源代码网推荐 byte[] data 源代码网推荐 ); 源代码网推荐 在第一个例子中,是通过拼字符串来得到byte[] data参数值的,对于这种表单显然不行,反过来想想,对于uploadData.aspx这样的程序来说,直接通过网页提交数据,后台所获取到的流是什么样的呢?(在我以前的一篇blog中,曾分析过这个问题:asp无组件上传进度条解决方案),最终的数据如下: 源代码网推荐 源代码网推荐 -----------------------------7d429871607fe 源代码网推荐 Content-Disposition: form-data; name="file1"; filename="G:homepage.txt" 源代码网推荐 Content-Type: text/plain 源代码网推荐 宝玉:http://www.webuc.net 源代码网推荐 -----------------------------7d429871607fe 源代码网推荐 Content-Disposition: form-data; name="filename" 源代码网推荐 default filename 源代码网推荐 -----------------------------7d429871607fe-- 源代码网推荐 源代码网推荐 源代码网推荐 所以只要拼一个这样的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 方法上载文件数据了。 源代码网推荐 源代码网推荐 具体代码如下: 源代码网推荐 生成二进制数据类的封装 源代码网推荐 源代码网推荐 using System; 源代码网推荐 using System.Web; 源代码网推荐 using System.IO; 源代码网推荐 using System.Net; 源代码网推荐 using System.Text; 源代码网推荐 using System.Collections; 源代码网推荐 源代码网推荐 namespace UploadData.Common 源代码网推荐 ...{ 源代码网推荐 /**//// <summary> 源代码网推荐 /// 创建WebClient.UploadData方法所需二进制数组 源代码网推荐 /// </summary> 源代码网推荐 public class CreateBytes 源代码网推荐 ...{ 源代码网推荐 Encoding encoding = Encoding.UTF8; 源代码网推荐 源代码网推荐 /**//// <summary> 源代码网推荐 /// 拼接所有的二进制数组为一个数组 源代码网推荐 /// </summary> 源代码网推荐 /// <param name="byteArrays">数组</param> 源代码网推荐 /// <returns></returns> 源代码网推荐 /// <remarks>加上结束边界</remarks> 源代码网推荐 public byte[] JoinBytes(ArrayList byteArrays) 源代码网推荐 ...{ 源代码网推荐 int length = 0; 源代码网推荐 int readLength = 0; 源代码网推荐 源代码网推荐 // 加上结束边界 源代码网推荐 string endBoundary = Boundary + "-- "; //结束边界 源代码网推荐 byte[] endBoundaryBytes = encoding.GetBytes(endBoundary); 源代码网推荐 byteArrays.Add(endBoundaryBytes); 源代码网推荐 源代码网推荐 foreach(byte[] b in byteArrays) 源代码网推荐 ...{ 源代码网推荐 length += b.Length; 源代码网推荐 } 源代码网推荐 byte[] bytes = new byte[length]; 源代码网推荐 源代码网推荐 // 遍历复制 源代码网推荐 // 源代码网推荐 foreach(byte[] b in byteArrays) 源代码网推荐 ...{ 源代码网推荐 b.CopyTo(bytes, readLength); 源代码网推荐 readLength += b.Length; 源代码网推荐 } 源代码网推荐 源代码网推荐 return bytes; 源代码网推荐 } 源代码网推荐 源代码网推荐 public bool UploadData(string uploadUrl, byte[] bytes, out byte[] responseBytes) 源代码网推荐 ...{ 源代码网推荐 WebClient webClient = new WebClient(); 源代码网推荐 webClient.Headers.Add("Content-Type", ContentType); 源代码网推荐 源代码网推荐 try 源代码网推荐 ...{ 源代码网推荐 responseBytes = webClient.UploadData(uploadUrl, bytes); 源代码网推荐 return true; 源代码网推荐 } 源代码网推荐 catch (WebException ex) 源代码网推荐 ...{ 源代码网推荐 Stream resp = ex.Response.GetResponseStream(); 源代码网推荐 responseBytes = new byte[ex.Response.ContentLength]; 源代码网推荐 resp.Read(responseBytes, 0, responseBytes.Length); 源代码网推荐 } 源代码网推荐 return false; 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 /**//// <summary> 源代码网推荐 /// 获取普通表单区域二进制数组 源代码网推荐 /// </summary> 源代码网推荐 /// <param name="fieldName">表单名</param> 源代码网推荐 /// <param name="fieldValue">表单值</param> 源代码网推荐 /// <returns></returns> 源代码网推荐 /// <remarks> 源代码网推荐 /// -----------------------------7d52ee27210a3c Content-Disposition: form-data; name="表单名" 表单值 源代码网推荐 /// </remarks> 源代码网推荐 public byte[] CreateFieldData(string fieldName, string fieldValue) 源代码网推荐 ...{ 源代码网推荐 string textTemplate = Boundary + " Content-Disposition: form-data; name="{0}" {1} "; 源代码网推荐 string text = String.Format(textTemplate, fieldName, fieldValue); 源代码网推荐 byte[] bytes = encoding.GetBytes(text); 源代码网推荐 return bytes; 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 /**//// <summary> 源代码网推荐 /// 获取文件上传表单区域二进制数组 源代码网推荐 /// </summary> 源代码网推荐 /// <param name="fieldName">表单名</param> 源代码网推荐 /// <param name="filename">文件名</param> 源代码网推荐 /// <param name="contentType">文件类型</param> 源代码网推荐 /// <param name="contentLength">文件长度</param> 源代码网推荐 /// <param name="stream">文件流</param> 源代码网推荐 /// <returns>二进制数组</returns> 源代码网推荐 public byte[] CreateFieldData(string fieldName, string filename,string contentType, byte[] fileBytes) 源代码网推荐 ...{ 源代码网推荐 string end = " "; 源代码网推荐 string textTemplate = Boundary + " Content-Disposition: form-data; name="{0}"; filename="{1}" Content-Type: {2} "; 源代码网推荐 源代码网推荐 // 头数据 源代码网推荐 string data = String.Format(textTemplate, fieldName, filename, contentType); 源代码网推荐 byte[] bytes = encoding.GetBytes(data); 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 // 尾数据 源代码网推荐 byte[] endBytes = encoding.GetBytes(end); 源代码网推荐 源代码网推荐 // 合成后的数组 源代码网推荐 byte[] fieldData = new byte[bytes.Length + fileBytes.Length + endBytes.Length]; 源代码网推荐 源代码网推荐 bytes.CopyTo(fieldData, 0); // 头数据 源代码网推荐 fileBytes.CopyTo(fieldData, bytes.Length); // 文件的二进制数据 源代码网推荐 endBytes.CopyTo(fieldData, bytes.Length + fileBytes.Length); // 源代码网推荐 源代码网推荐 return fieldData; 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 属性#region 属性 源代码网推荐 public string Boundary 源代码网推荐 ...{ 源代码网推荐 get 源代码网推荐 ...{ 源代码网推荐 string[] bArray, ctArray; 源代码网推荐 string contentType = ContentType; 源代码网推荐 ctArray = contentType.Split(";"); 源代码网推荐 if (ctArray[0].Trim().ToLower() == "multipart/form-data") 源代码网推荐 ...{ 源代码网推荐 bArray = ctArray[1].Split("="); 源代码网推荐 return "--" + bArray[1]; 源代码网推荐 } 源代码网推荐 return null; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 public string ContentType 源代码网推荐 ...{ 源代码网推荐 get ...{ 源代码网推荐 if (HttpContext.Current == null) 源代码网推荐 ...{ 源代码网推荐 return "multipart/form-data; boundary=---------------------------7d5b915500cee"; 源代码网推荐 } 源代码网推荐 return HttpContext.Current.Request.ContentType; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 #endregion 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 在Winform中调用 源代码网推荐 源代码网推荐 源代码网推荐 using System; 源代码网推荐 using System.Drawing; 源代码网推荐 using System.Collections; 源代码网推荐 using System.ComponentModel; 源代码网推荐 using System.Windows.Forms; 源代码网推荐 using System.Data; 源代码网推荐 源代码网推荐 using UploadData.Common; 源代码网推荐 using System.IO; 源代码网推荐 源代码网推荐 namespace UploadDataWin 源代码网推荐 ...{ 源代码网推荐 /**//// <summary> 源代码网推荐 /// frmUpload 的摘要说明。 源代码网推荐 /// </summary> 源代码网推荐 public class frmUpload : System.Windows.Forms.Form 源代码网推荐 ...{ 源代码网推荐 private System.Windows.Forms.Label lblAmigoToken; 源代码网推荐 private System.Windows.Forms.TextBox txtAmigoToken; 源代码网推荐 private System.Windows.Forms.Label lblFilename; 源代码网推荐 private System.Windows.Forms.TextBox txtFilename; 源代码网推荐 private System.Windows.Forms.Button btnBrowse; 源代码网推荐 private System.Windows.Forms.TextBox txtFileData; 源代码网推荐 private System.Windows.Forms.Label lblFileData; 源代码网推荐 private System.Windows.Forms.Button btnUpload; 源代码网推荐 private System.Windows.Forms.OpenFileDialog openFileDialog1; 源代码网推荐 private System.Windows.Forms.TextBox txtResponse; 源代码网推荐 /**//// <summary> 源代码网推荐 /// 必需的设计器变量。 源代码网推荐 /// </summary> 源代码网推荐 private System.ComponentModel.Container components = null; 源代码网推荐 源代码网推荐 public frmUpload() 源代码网推荐 ...{ 源代码网推荐 // 源代码网推荐 // Windows 窗体设计器支持所必需的 源代码网推荐 // 源代码网推荐 InitializeComponent(); 源代码网推荐 源代码网推荐 // 源代码网推荐 // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 源代码网推荐 // 源代码网推荐 } 源代码网推荐 源代码网推荐 /**//// <summary> 源代码网推荐 /// 清理所有正在使用的资源。 源代码网推荐 /// </summary> 源代码网推荐 protected override void Dispose( bool disposing ) 源代码网推荐 ...{ 源代码网推荐 if( disposing ) 源代码网推荐 ...{ 源代码网推荐 if (components != null) 源代码网推荐 ...{ 源代码网推荐 components.Dispose(); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 base.Dispose( disposing ); 源代码网推荐 } 源代码网推荐 源代码网推荐 Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码 源代码网推荐 /**//// <summary> 源代码网推荐 /// 设计器支持所需的方法 - 不要使用代码编辑器修改 源代码网推荐 /// 此方法的内容。 源代码网推荐 /// </summary> 源代码网推荐 private void InitializeComponent() 源代码网推荐 ...{ 源代码网推荐 this.lblAmigoToken = new System.Windows.Forms.Label(); 源代码网推荐 this.txtAmigoToken = new System.Windows.Forms.TextBox(); 源代码网推荐 this.lblFilename = new System.Windows.Forms.Label(); 源代码网推荐 this.txtFilename = new System.Windows.Forms.TextBox(); 源代码网推荐 this.btnBrowse = new System.Windows.Forms.Button(); 源代码网推荐 this.txtFileData = new System.Windows.Forms.TextBox(); 源代码网推荐 this.lblFileData = new System.Windows.Forms.Label(); 源代码网推荐 this.btnUpload = new System.Windows.Forms.Button(); 源代码网推荐 this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); 源代码网推荐 this.txtResponse = new System.Windows.Forms.TextBox(); 源代码网推荐 this.SuspendLayout(); 源代码网推荐 // 源代码网推荐 // lblAmigoToken 源代码网推荐 // 源代码网推荐 this.lblAmigoToken.Location = new System.Drawing.Point(40, 48); 源代码网推荐 this.lblAmigoToken.Name = "lblAmigoToken"; 源代码网推荐 this.lblAmigoToken.Size = new System.Drawing.Size(72, 23); 源代码网推荐 this.lblAmigoToken.TabIndex = 0; 源代码网推荐 this.lblAmigoToken.Text = "AmigoToken"; 源代码网推荐 // 源代码网推荐 // txtAmigoToken 源代码网推荐 // 源代码网推荐 this.txtAmigoToken.Location = new System.Drawing.Point(120, 48); 源代码网推荐 this.txtAmigoToken.Name = "txtAmigoToken"; 源代码网推荐 this.txtAmigoToken.Size = new System.Drawing.Size(248, 21); 源代码网推荐 this.txtAmigoToken.TabIndex = 1; 源代码网推荐 this.txtAmigoToken.Text = ""; 源代码网推荐 // 源代码网推荐 // lblFilename 源代码网推荐 // 源代码网推荐 this.lblFilename.Location = new System.Drawing.Point(40, 96); 源代码网推荐 this.lblFilename.Name = "lblFilename"; 源代码网推荐 this.lblFilename.Size = new System.Drawing.Size(80, 23); 源代码网推荐 this.lblFilename.TabIndex = 2; 源代码网推荐 this.lblFilename.Text = "Filename"; 源代码网推荐 // 源代码网推荐 // txtFilename 源代码网推荐 // 源代码网推荐 this.txtFilename.Location = new System.Drawing.Point(120, 96); 源代码网推荐 this.txtFilename.Name = "txtFilename"; 源代码网推荐 this.txtFilename.Size = new System.Drawing.Size(248, 21); 源代码网推荐 this.txtFilename.TabIndex = 3; 源代码网推荐 this.txtFilename.Text = ""; 源代码网推荐 // 源代码网推荐 // btnBrowse 源代码网推荐 // 源代码网推荐 this.btnBrowse.Location = new System.Drawing.Point(296, 144); 源代码网推荐 this.btnBrowse.Name = "btnBrowse"; 源代码网推荐 this.btnBrowse.TabIndex = 4; 源代码网推荐 this.btnBrowse.Text = "浏览..."; 源代码网推荐 this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click); 源代码网推荐 // 源代码网推荐 // txtFileData 源代码网推荐 // 源代码网推荐 this.txtFileData.Location = new System.Drawing.Point(120, 144); 源代码网推荐 this.txtFileData.Name = "txtFileData"; 源代码网推荐 this.txtFileData.Size = new System.Drawing.Size(168, 21); 源代码网推荐 this.txtFileData.TabIndex = 5; 源代码网推荐 this.txtFileData.Text = ""; 源代码网推荐 // 源代码网推荐 // lblFileData 源代码网推荐 // 源代码网推荐 this.lblFileData.Location = new System.Drawing.Point(40, 144); 源代码网推荐 this.lblFileData.Name = "lblFileData"; 源代码网推荐 this.lblFileData.Size = new System.Drawing.Size(72, 23); 源代码网推荐 this.lblFileData.TabIndex = 6; 源代码网推荐 this.lblFileData.Text = "FileData"; 源代码网推荐 // 源代码网推荐 // btnUpload 源代码网推荐 // 源代码网推荐 this.btnUpload.Location = new System.Drawing.Point(48, 184); 源代码网推荐 this.btnUpload.Name = "btnUpload"; 源代码网推荐 this.btnUpload.TabIndex = 7; 源代码网推荐 this.btnUpload.Text = "Upload"; 源代码网推荐 this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click); 源代码网推荐 // 源代码网推荐 // txtResponse 源代码网推荐 // 源代码网推荐 this.txtResponse.Location = new System.Drawing.Point(136, 184); 源代码网推荐 this.txtResponse.Multiline = true; 源代码网推荐 this.txtResponse.Name = "txtResponse"; 源代码网推荐 this.txtResponse.Size = new System.Drawing.Size(248, 72); 源代码网推荐 this.txtResponse.TabIndex = 8; 源代码网推荐 this.txtResponse.Text = ""; 源代码网推荐 // 源代码网推荐 // frmUpload 源代码网推荐 // 源代码网推荐 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); 源代码网推荐 this.ClientSize = new System.Drawing.Size(400, 269); 源代码网推荐 this.Controls.Add(this.txtResponse); 源代码网推荐 this.Controls.Add(this.btnUpload); 源代码网推荐 this.Controls.Add(this.lblFileData); 源代码网推荐 this.Controls.Add(this.txtFileData); 源代码网推荐 this.Controls.Add(this.btnBrowse); 源代码网推荐 this.Controls.Add(this.txtFilename); 源代码网推荐 this.Controls.Add(this.lblFilename); 源代码网推荐 this.Controls.Add(this.txtAmigoToken); 源代码网推荐 this.Controls.Add(this.lblAmigoToken); 源代码网推荐 this.Name = "frmUpload"; 源代码网推荐 this.Text = "frmUpload"; 源代码网推荐 this.ResumeLayout(false); 源代码网推荐 源代码网推荐 } 源代码网推荐 #endregion 源代码网推荐 源代码网推荐 /**//// <summary> 源代码网推荐 /// 应用程序的主入口点。 源代码网推荐 /// </summary> 源代码网推荐 [STAThread] 源代码网推荐 static void Main() 源代码网推荐 ...{ 源代码网推荐 Application.Run(new frmUpload()); 源代码网推荐 } 源代码网推荐 源代码网推荐 private void btnUpload_Click(object sender, System.EventArgs e) 源代码网推荐 ...{ 源代码网推荐 // 非空检验 源代码网推荐 if (txtAmigoToken.Text.Trim() == "" || txtFilename.Text == "" || txtFileData.Text.Trim() == "") 源代码网推荐 ...{ 源代码网推荐 MessageBox.Show("Please fill data"); 源代码网推荐 return; 源代码网推荐 } 源代码网推荐 源代码网推荐 // 所要上传的文件路径 源代码网推荐 string path = txtFileData.Text.Trim(); 源代码网推荐 源代码网推荐 // 检查文件是否存在 源代码网推荐 if (!File.Exists(path)) 源代码网推荐 ...{ 源代码网推荐 MessageBox.Show("{0} does not exist!", path); 源代码网推荐 return; 源代码网推荐 } 源代码网推荐 源代码网推荐 // 读文件流 源代码网推荐 FileStream fs = new FileStream(path, FileMode.Open, 源代码网推荐 FileAccess.Read, FileShare.Read); 源代码网推荐 源代码网推荐 // 这部分需要完善 源代码网推荐 string ContentType = "application/octet-stream"; 源代码网推荐 byte[] fileBytes = new byte[fs.Length]; 源代码网推荐 fs.Read(fileBytes, 0, Convert.ToInt32(fs.Length)); 源代码网推荐 源代码网推荐 源代码网推荐 // 生成需要上传的二进制数组 源代码网推荐 CreateBytes cb = new CreateBytes(); 源代码网推荐 // 所有表单数据 源代码网推荐 ArrayList bytesArray = new ArrayList(); 源代码网推荐 // 普通表单 源代码网推荐 bytesArray.Add(cb.CreateFieldData("FileName", txtFilename.Text)); 源代码网推荐 bytesArray.Add(cb.CreateFieldData("AmigoToken", txtAmigoToken.Text)); 源代码网推荐 // 文件表单 源代码网推荐 bytesArray.Add(cb.CreateFieldData("FileData", path 源代码网推荐 , ContentType, fileBytes)); 源代码网推荐 源代码网推荐 // 合成所有表单并生成二进制数组 源代码网推荐 byte[] bytes = cb.JoinBytes(bytesArray); 源代码网推荐 源代码网推荐 // 返回的内容 源代码网推荐 byte[] responseBytes; 源代码网推荐 源代码网推荐 // 上传到指定Url 源代码网推荐 bool uploaded = cb.UploadData("http://localhost/UploadData/UploadAvatar.aspx", bytes, out responseBytes); 源代码网推荐 源代码网推荐 // 将返回的内容输出到文件 源代码网推荐 using (FileStream file = new FileStream(@"c: esponse.text", FileMode.Create, FileAccess.Write, FileShare.Read)) 源代码网推荐 ...{ 源代码网推荐 file.Write(responseBytes, 0, responseBytes.Length); 源代码网推荐 } 源代码网推荐 源代码网推荐 txtResponse.Text = System.Text.Encoding.UTF8.GetString(responseBytes); 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 private void btnBrowse_Click(object sender, System.EventArgs e) 源代码网推荐 ...{ 源代码网推荐 if(openFileDialog1.ShowDialog() == DialogResult.OK) 源代码网推荐 ...{ 源代码网推荐 txtFileData.Text = openFileDialog1.FileName; 源代码网推荐 } 源代码网推荐 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 完整的代码见附件: UploadData.rar(38K)(http://bbs.openlab.net.cn/PostAttachment.aspx?PostID=400927),解压后给web目录建个虚拟目录"UploadData",其中UploadAvatar.aspx是实际的上传处理页,如果上传成功,则返回文件名和文件类型等信息。default.aspx是asp.net页面来调用 WebClient.UploadData方法提交数据,UploadDataWin项目则是winform程序调用。 源代码网推荐 源代码网推荐 宝玉的blog: http://blog.joycode.com/dotey 源代码网推荐 源代码网推荐 源代码网推荐 源代码网供稿. |
