当前位置:中国站长下载文章中心网页编程.NET编程 → 用 WebClient.UploadData 方法 上载文件数据

用 WebClient.UploadData 方法 上载文件数据

减小字体 增大字体 作者:不详  来源:不详  发布时间:2006-8-14 9:54:40
plate, fieldName, fieldValue);
   byte[] bytes = encoding.GetBytes(text);
   return bytes;
   }
  
  
   /**//// <summary>
   /// 获取文件上传表单区域二进制数组
   /// </summary>
   /// <param name="fieldName">表单名</param>
   /// <param name="filename">文件名</param>
   /// <param name="contentType">文件类型</param>
   /// <param name="contentLength">文件长度</param>
   /// <param name="stream">文件流</param>
   /// <returns>二进制数组</returns>
   public byte[] CreateFieldData(string fieldName, string filename,string contentType, byte[] fileBytes)
   ...{
   string end = "\r\n";
   string textTemplate = Boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
  
   // 头数据
   string data = String.Format(textTemplate, fieldName, filename, contentType);
   byte[] bytes = encoding.GetBytes(data);
  
  
  
   // 尾数据
   byte[] endBytes = encoding.GetBytes(end);
  
   // 合成后的数组
   byte[] fieldData = new byte[bytes.Length + fileBytes.Length + endBytes.Length];
  
   bytes.CopyTo(fieldData, 0); // 头数据
   fileBytes.CopyTo(fieldData, bytes.Length); // 文件的二进制数据
   endBytes.CopyTo(fieldData, bytes.Length + fileBytes.Length); // \r\n
  
   return fieldData;
   }
  
  
   属性#region 属性
   public string Boundary
   ...{
   get
   ...{
   string[] bArray, ctArray;
   string contentType = ContentType;
   ctArray = contentType.Split(';');
   if (ctArray[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);

上一页  [1] [2] [3] [4]  下一页