使用WebClient实现文件上传

  1<xmp>   
  2using System;   
  3using System.Drawing;   
  4using System.Collections;   
  5using System.ComponentModel;   
  6using System.Windows.Forms;   
  7using System.Data;   
  8using System.IO;   
  9using System.Net;   
 10using System.Threading; 
 11
 12namespace UpLoad   
 13{   
 14/// <summary>   
 15/// Form1 的摘要说明。   
 16/// </summary>   
 17public class Form1 : System.Windows.Forms.Form   
 18{   
 19private System.Windows.Forms.Label label1;   
 20private System.Windows.Forms.Label label2;   
 21private System.Windows.Forms.TextBox txtURI;   
 22private System.Windows.Forms.Button btnSelect;   
 23private System.Windows.Forms.Button btnUpLoad;   
 24private System.Windows.Forms.StatusBar stBar;   
 25private System.Windows.Forms.OpenFileDialog openFile;   
 26private System.Windows.Forms.ListBox lstFiles;   
 27private System.Windows.Forms.Button btnDelete;   
 28private WebClient client=new WebClient();   
 29/// <summary>   
 30/// 必需的设计器变量。   
 31/// </summary>   
 32private System.ComponentModel.Container components = null; 
 33
 34public Form1()   
 35{   
 36//   
 37// Windows 窗体设计器支持所必需的   
 38//   
 39InitializeComponent(); 
 40
 41//   
 42// TODO: 在 InitializeComponent 调用后添加任何构造函数代码   
 43//   
 44} 
 45
 46/// <summary>   
 47/// 清理所有正在使用的资源。   
 48/// </summary>   
 49protected override void Dispose( bool disposing )   
 50{   
 51if( disposing )   
 52{   
 53if (components != null)   
 54{   
 55components.Dispose();   
 56}   
 57}   
 58base.Dispose( disposing );   
 59} 
 60
 61#region Windows 窗体设计器生成的代码   
 62/// <summary>   
 63/// 设计器支持所需的方法 - 不要使用代码编辑器修改   
 64/// 此方法的内容。   
 65/// </summary>   
 66private void InitializeComponent()   
 67{   
 68this.label1 = new System.Windows.Forms.Label();   
 69this.label2 = new System.Windows.Forms.Label();   
 70this.txtURI = new System.Windows.Forms.TextBox();   
 71this.btnSelect = new System.Windows.Forms.Button();   
 72this.btnUpLoad = new System.Windows.Forms.Button();   
 73this.stBar = new System.Windows.Forms.StatusBar();   
 74this.openFile = new System.Windows.Forms.OpenFileDialog();   
 75this.lstFiles = new System.Windows.Forms.ListBox();   
 76this.btnDelete = new System.Windows.Forms.Button();   
 77this.SuspendLayout();   
 78//   
 79// label1   
 80//   
 81this.label1.Location = new System.Drawing.Point(24, 15);   
 82this.label1.Name = "label1";   
 83this.label1.Size = new System.Drawing.Size(48, 23);   
 84this.label1.TabIndex = 0;   
 85this.label1.Text = "上传到:";   
 86//   
 87// label2   
 88//   
 89this.label2.Location = new System.Drawing.Point(8, 56);   
 90this.label2.Name = "label2";   
 91this.label2.Size = new System.Drawing.Size(64, 23);   
 92this.label2.TabIndex = 1;   
 93this.label2.Text = "上传文件:";   
 94//   
 95// txtURI   
 96//   
 97this.txtURI.Location = new System.Drawing.Point(80, 16);   
 98this.txtURI.Name = "txtURI";   
 99this.txtURI.Size = new System.Drawing.Size(256, 21);   
100this.txtURI.TabIndex = 2;   
101this.txtURI.Text = "";   
102//   
103// btnSelect   
104//   
105this.btnSelect.Location = new System.Drawing.Point(336, 56);   
106this.btnSelect.Name = "btnSelect";   
107this.btnSelect.TabIndex = 4;   
108this.btnSelect.Text = "选择...";   
109this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);   
110//   
111// btnUpLoad   
112//   
113this.btnUpLoad.Location = new System.Drawing.Point(336, 120);   
114this.btnUpLoad.Name = "btnUpLoad";   
115this.btnUpLoad.TabIndex = 5;   
116this.btnUpLoad.Text = "开始上传";   
117this.btnUpLoad.Click += new System.EventHandler(this.btnUpLoad_Click);   
118//   
119// stBar   
120//   
121this.stBar.Location = new System.Drawing.Point(0, 176);   
122this.stBar.Name = "stBar";   
123this.stBar.ShowPanels = true;   
124this.stBar.Size = new System.Drawing.Size(424, 22);   
125this.stBar.TabIndex = 6;   
126//   
127// openFile   
128//   
129this.openFile.Multiselect = true;   
130this.openFile.Title = "选择上传问文件";   
131//   
132// lstFiles   
133//   
134this.lstFiles.ItemHeight = 12;   
135this.lstFiles.Location = new System.Drawing.Point(80, 56);   
136this.lstFiles.Name = "lstFiles";   
137this.lstFiles.Size = new System.Drawing.Size(256, 88);   
138this.lstFiles.TabIndex = 7;   
139//   
140// btnDelete   
141//   
142this.btnDelete.Location = new System.Drawing.Point(336, 88);   
143this.btnDelete.Name = "btnDelete";   
144this.btnDelete.TabIndex = 8;   
145this.btnDelete.Text = "从列表删除";   
146this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);   
147//   
148// Form1   
149//   
150this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);   
151this.ClientSize = new System.Drawing.Size(424, 198);   
152this.Controls.Add(this.btnDelete);   
153this.Controls.Add(this.lstFiles);   
154this.Controls.Add(this.stBar);   
155this.Controls.Add(this.btnUpLoad);   
156this.Controls.Add(this.btnSelect);   
157this.Controls.Add(this.txtURI);   
158this.Controls.Add(this.label2);   
159this.Controls.Add(this.label1);   
160this.Name = "Form1";   
161this.Text = "Form1";   
162this.ResumeLayout(false); 
163
164}   
165#endregion 
166
167/// <summary>   
168/// 应用程序的主入口点。   
169/// </summary>   
170[STAThread]   
171static void Main()   
172{   
173Application.Run(new Form1());   
174} 
175
176private void btnSelect_Click(object sender, System.EventArgs e)   
177{   
178if(openFile.ShowDialog()==DialogResult.Cancel)   
179{   
180return;   
181}   
182for(int i=0;i<openfile.filenames.length;i++) +i.tostring()+"个文件!";="" +lstfiles.items[j].tostring();="" ;="" <="" btndelete_click(object="" btnupload.enabled="true;" btnupload_click(object="" catch(webexception="" client.uploadfile(url,"post",lstfiles.items[j].tostring());="" e)="" ex)="" finally="" for(int="" i="0;" i++;="" if(!lstfiles.items.contains(openfile.filenames[i]))="" if(lstfiles.selectedindex="-1)" int="" j="0;j&lt;lstFiles.Items.Count;j++)" lstfiles.items.add(openfile.filenames[i]);="" lstfiles.items.removeat(lstfiles.selectedindex);="" messagebox.show(ex.message,"error");="" path="string.Empty;" private="" return;="" sender,="" startupload()="" stbar.text="上传中断" string="" system.eventargs="" th="new" th.start();="" thread="" thread(new="" threadstart(startupload));="" try="" url="txtURI.Text;" void="" xmp="" {="" }=""></openfile.filenames.length;i++)></xmp>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus