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

今天看了TERRYLEE老大的一篇写上传的.里面有个WEBCLIENT我就上网查了一下,找到孟子大人的一篇文章.感觉不错,就转了过来.大家一起学习下. 
.NET 提供了许多上载文件的方法,在Windows Form应用程序中,我们可以使用WebClient类来实现。WebClient类也有两个方法可以上载,UploadFileOpenWrite方法,下面就是一个实际的例子,两种方法都有代码:

C#代码如下:

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()); } /// /// .NET SDK 上面的打开文件的类 /// 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 = "正在上传文件,请稍侯!"; } } }
剩下说的我的感受吧.把代码下下来.建立新项目.测试了一下可以用.关键就是上传那是关键,做个标记,以后多学习.

转载于:https://www.cnblogs.com/lxinxuan/archive/2006/09/05/495182.html

利用WebClient类向服务器上载文件(转孟子的)相关推荐

  1. C#利用WebClient 两种方式下载文件(一)

    WebClient client = new WebClient(); 第一种 string URLAddress = @"http://files.cnblogs.com/x4646/tr ...

  2. c#利用WebClient和WebRequest获取网页源代码的比较

    前几天举例分析了用asp+xmlhttp获取网页源代码的方法,但c#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取 ...

  3. C#中用WebClient.UploadData 方法上载文件数据

    假如某网站有个表单,例如(url: http://localhost/login.aspx): 帐号 密码 我们需要在程序中提交数据到这个表单,对于这种表单,我们可以使用 WebClient.Uplo ...

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

    如某网站有个表单,例如(url: http://localhost/login.aspx): 帐号 密码 我们需要在程序中提交数据到这个表单,对于这种表单,我们可以使用 WebClient.Uploa ...

  5. .Net框架集WebClient类向WinCE平台上传文件(FTP方式)延迟15秒释疑

    年前在做嵌入式组态时,有一个组态下载功能,该功能就是采用FTP上传功能把组态配置文件上传到触摸屏(WinCE4.2平台,已开FTP服务器)中的.所用到的指令就是WebClient类的UploadFil ...

  6. Linux下利用rsync实现多服务器文件同步

    Linux下利用rsync实现多服务器文件同步 目标:多服务器文件同步 环境:2台centos5.6 Web端:192.168.20.20 Backup端:192.168.20.21 需要备份目录为: ...

  7. 利用ASP.NET向服务器上传文件[转]

    文件上传技术是一个很实用的技术,有着很广泛的应用,在ASP.NET自身的前一个版本ASP里实现这个功能,就必须使用第三方的组件或者自己开发组件了,现在,用ASP.NET实现起来就简单得多了,我们不需要 ...

  8. 服务器文件夹大小查看器,asp利用FSO检测服务器空间文件夹大小

    asp利用FSO检测服务器空间文件夹大小 * {margin:0;padding:0;font-size:12px;} body {padding:15px;} .size1 {font-weight ...

  9. linux smb视频,利用 CentOS 7 samba 服务器与 ES 文件浏览器实现手机端在线播放电脑端视频...

    环境 以下环境仅代表本文测试环境,其它版本应该也可以. 虚拟机 Linux: CentOS Linux release 7.4.1708 (Core) 物理机 Windows: Windows 10 ...

最新文章

  1. 从find_vma和find_vma_prev看内核
  2. 同一个网站别人能打开我打不开_做网站建设需要注意的五大事项
  3. 指针:自定义函数sumDiff(),调用它来求两个数的和、差
  4. ksnapshot运行look up error undefined symbol错误解决方案
  5. emacs 新手笔记(四) —— 使用 dired 完成一些简单的文件和目录操作
  6. leetcode刷题集:栈与队列
  7. java使用:: 表达式_Java 12:开关表达式
  8. HDU 6188 Duizi and Shunzi
  9. Angular CLI 安装
  10. PageHelper 分页Total总是为pageSize的问题
  11. 电脑发短信_让电脑自动给老婆发短信?!这个懒到极致的大神,我是服了...
  12. IT专业沦为新时代农民工?为什么你身边的人都不让你报计算机专业?
  13. python公共变量声明_Python变量声明
  14. spring boot + vue + element-ui全栈开发入门——windows开发环境
  15. SQLite学习笔记(二)--VC调用环境搭建
  16. VB6.0鼠标注册详细教程
  17. 项目实战:小米商城官网及秒杀高仿(html+css+js)
  18. 白鹭引擎王泽:重度H5游戏性能优化技巧
  19. arm鲲鹏服务器和x86区别
  20. 五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”

热门文章

  1. Python(四)字符串
  2. Oracle 11.2.0.4 x64 RAC扩展存储空间
  3. Jquery的$命名冲突
  4. 机动车号牌图像自动识别技术规范
  5. Eclipse插件打开编辑器
  6. [算法进阶0x10]基本数据结构C作业总结
  7. 局域网语音通话demo
  8. 【python】命令行解析工具getopt用法
  9. 数据结构(莫队算法):国家集训队2010 小Z的袜子
  10. 【运营】各大电商七夕活动对比