分享
 
 
 

利用WebClient类向服务器上载文件(转)

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Net;

using System.Text;

using System.IO;

namespace UploadFile

{

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Label label1;

private System.Windows.Forms.Label label2;

private System.Windows.Forms.Button button1;

private System.Windows.Forms.Label label3;

private System.Windows.Forms.Button button2;

private System.Windows.Forms.TextBox txtFileName;

private System.Windows.Forms.TextBox txtServerPath;

private System.Windows.Forms.LinkLabel linkLabel1;

private System.ComponentModel.Container components = null;

public Form1()

{

InitializeComponent();

}

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

private void InitializeComponent()

{

System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));

this.label1 = new System.Windows.Forms.Label();

this.txtServerPath = new System.Windows.Forms.TextBox();

this.label2 = new System.Windows.Forms.Label();

this.button1 = new System.Windows.Forms.Button();

this.txtFileName = new System.Windows.Forms.TextBox();

this.label3 = new System.Windows.Forms.Label();

this.button2 = new System.Windows.Forms.Button();

this.linkLabel1 = new System.Windows.Forms.LinkLabel();

this.SuspendLayout();

//

// label1

//

this.label1.ForeColor = System.Drawing.Color.Red;

this.label1.Location = new System.Drawing.Point(8, 96);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(448, 16);

this.label1.TabIndex = 0;

this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

//

// txtServerPath

//

this.txtServerPath.Location = new System.Drawing.Point(128, 8);

this.txtServerPath.Name = "txtServerPath";

this.txtServerPath.Size = new System.Drawing.Size(320, 21);

this.txtServerPath.TabIndex = 1;

this.txtServerPath.Text = "http://mengxianhui/aspxWeb/Images/";;

//

// label2

//

this.label2.AutoSize = true;

this.label2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold,

System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));

this.label2.ForeColor = System.Drawing.Color.Navy;

this.label2.Location = new System.Drawing.Point(8, 12);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(116, 17);

this.label2.TabIndex = 2;

this.label2.Text = "请输入服务器地址:";

this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

//

// button1

//

this.button1.Location = new System.Drawing.Point(192, 64);

this.button1.Name = "button1";

this.button1.TabIndex = 3;

this.button1.Text = "上载文件";

this.button1.Click += new System.EventHandler(this.button1_Click);

this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);

//

// txtFileName

//

this.txtFileName.Location = new System.Drawing.Point(128, 32);

this.txtFileName.Name = "txtFileName";

this.txtFileName.Size = new System.Drawing.Size(232, 21);

this.txtFileName.TabIndex = 4;

this.txtFileName.Text = "";

//

// label3

//

this.label3.AutoSize = true;

this.label3.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold,

System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));

this.label3.ForeColor = System.Drawing.Color.DarkBlue;

this.label3.Location = new System.Drawing.Point(8, 38);

this.label3.Name = "label3";

this.label3.Size = new System.Drawing.Size(116, 17);

this.label3.TabIndex = 5;

this.label3.Text = "请输入上传文件名:";

this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

//

// button2

//

this.button2.Location = new System.Drawing.Point(370, 32);

this.button2.Name = "button2";

this.button2.Size = new System.Drawing.Size(80, 23);

this.button2.TabIndex = 6;

this.button2.Text = "浏览文件…";

this.button2.Click += new System.EventHandler(this.button2_Click);

//

// linkLabel1

//

this.linkLabel1.Location = new System.Drawing.Point(16, 120);

this.linkLabel1.Name = "linkLabel1";

this.linkLabel1.Size = new System.Drawing.Size(440, 24);

this.linkLabel1.TabIndex = 7;

this.linkLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

this.linkLabel1.LinkClicked += new

System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.BackColor = System.Drawing.SystemColors.Control;

this.ClientSize = new System.Drawing.Size(464, 157);

this.Controls.Add(this.linkLabel1);

this.Controls.Add(this.button2);

this.Controls.Add(this.txtFileName);

this.Controls.Add(this.label3);

this.Controls.Add(this.txtServerPath);

this.Controls.Add(this.label2);

this.Controls.Add(this.button1);

this.Controls.Add(this.label1);

this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

this.Name = "Form1";

this.Text = "WebClient 上传文件的例子";

this.Resize += new System.EventHandler(this.Form1_Resize);

this.ResumeLayout(false);

}

#endregion

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

/// <summary>

/// .NET SDK 上面的打开文件的类

/// </summary>

private FileStream OpenFile()

{

OpenFileDialog dlgOpenFile = new OpenFileDialog();

dlgOpenFile.ShowReadOnly = true;

if(dlgOpenFile.ShowDialog() == DialogResult.OK)

{

if(dlgOpenFile.ReadOnlyChecked == true)

{

return (FileStream)dlgOpenFile.OpenFile();

}

else

{

string path = dlgOpenFile.FileName;

return new FileStream(path, System.IO.FileMode.Open,

System.IO.FileAccess.ReadWrite);

}

}

return null;

}

private void button2_Click(object sender, System.EventArgs e)

{

OpenFileDialog dlgOpenFile = new OpenFileDialog();

dlgOpenFile.InitialDirectory = @"C:\";

dlgOpenFile.ShowReadOnly = false;

dlgOpenFile.ReadOnlyChecked = true;

dlgOpenFile.Filter = "所有文件 (*.*)|*.*";

if(dlgOpenFile.ShowDialog() == DialogResult.OK)

{

if(dlgOpenFile.ReadOnlyChecked == true)

{

txtFileName.Text = dlgOpenFile.FileName.ToString();

}

}

}

private void button1_Click(object sender, System.EventArgs e)

{

// 需要注意的是:txtServerPath文件夹有匿名可写的权限。

// 可以自己定义新文件名字

if(txtFileName.Text.Trim() == "" || txtServerPath.Text.Trim() == "")

{

MessageBox.Show("请输入你要上载的文件名字!","错误:", MessageBoxButtons.OK,

MessageBoxIcon.Information);

}

else

{

/// 得到文件名,文件扩展名字,服务器路径

string fileNamePath = txtFileName.Text.Trim();

string uriString = txtServerPath.Text.Trim();

string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1);

string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") + 1);

if(uriString.EndsWith("/") == false) uriString = uriString + "/";

uriString = uriString + fileName;

/// 创建WebClient实例

WebClient myWebClient = new WebClient();

myWebClient.Credentials = CredentialCache.DefaultCredentials;

// 要上传的文件

FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);

//FileStream fs = OpenFile();

BinaryReader r = new BinaryReader(fs);

try

{

//使用UploadFile方法可以用下面的格式

//myWebClient.UploadFile(uriString,"PUT",fileNamePath);

byte[] postArray = r.ReadBytes((int)fs.Length);

Stream postStream = myWebClient.OpenWrite(uriString,"PUT");

if(postStream.CanWrite)

{

postStream.Write(postArray,0,postArray.Length);

label1.Text = fileName + "上传成功!";

}

else

{

label1.Text = "文件目前不可写!";

}

postStream.Close();

linkLabel1.Text = "查看上载的文件";

for(int i = linkLabel1.Links.Count - 1;i>-1;i--)

linkLabel1.Links.Remove(linkLabel1.Links[i]);

linkLabel1.Links.Add(0,linkLabel1.Text.Length,uriString);

}

catch(WebException errMsg)

{

label1.Text="上传失败:" + errMsg.Message;

}

}

}

private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)

{

this.WindowState = FormWindowState.Minimized;

this.linkLabel1.Links[linkLabel1.Links.IndexOf(e.Link)].Visited = true;

string target = e.Link.LinkData as string;

if(null != target)

{

System.Diagnostics.Process.Start(target);

}

else

{

MessageBox.Show("请用浏览器访问:" + target);

}

}

private void Form1_Resize(object sender, System.EventArgs e)

{

if(this.WindowState == FormWindowState.Maximized) this.WindowState = FormWindowState.Normal;

}

private void button1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

{

if(txtFileName.Text.Trim() != "" && txtServerPath.Text.Trim() != "")

label1.Text = "正在上传文件,请稍侯……!";

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有