| 利用WebClient类向服务器上载文件 |
| 作者: 孟宪会 出自: 【孟宪会之精彩世界】 发布日期: 2003-7-30 17:44:10
|
| .NET 提供了许多上载文件的方法,在Windows Form应用程序中,我们可以使用WebClient类来实现。WebClient类也有两个方法可以上载,UploadFile和OpenWrite方法,下面就是一个实际的例子,两种方法都有代码: 结果如下:
C#代码如下: <xmp>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 = "正在上传文件,请稍侯……!"; } } } </xmp> |
相关推荐
PDF批量加水印 与 去除水印实践
我们可以给PDF加图片水印或文字水印,要去除图片水印,一般只需要删除最后一个图片对象即可。要去除文字水印,需要保证主体内容和文字水印在/Contents中位于不同的对象内,这样我们只需要删除文字水印对应的对象即可删除水印。而对于主体内容和文字水印已经混合在一个对象时,本文的提供的方法则无能为力,需要进一步深入分析PDF细节。
【Linux】文件上传到Linux服务器常用方法
本文介绍几种常见的方法,把文件上传到Linux服务器中!常见有使用:scp命令、xshell软件里的xftp程序、U盘挂载、服务器自带的lrzsz程序。
随机子空间集成方法原理与scikit-learn实践
集成学习通过组合多个基学习器的预测结果来提升模型性能,是机器学习中的核心方法之一。随机子空间方法作为集成学习的重要变体,其核心原理是通过对特征空间进行随机子采样来增加模型多样性。这种方法特别适合处理高维数据,能有效缓解维度灾难问题,常与决策树、K近邻等对特征选择敏感的基学习器配合使用。在scikit-learn中,可以通过BaggingClassifier和BaggingRegressor实现随机子空间集成,关键参数包括max_features和n_estimators。实际应用中,该方法在金融风控、医疗诊
SPRINGBOOT 实现大文件上传下载、分片、断点续传教程
该说不说,最近这块好像挻火的,今天早上又有网友加我微信,也是想了解一下这块的技术和方案,也不知道这位老哥是从哪里找到的我的微信号,虽然在网上已经公开了,但是很多人还是找不到。 昨天晚上论坛里一位网友发私信给我,咨询了这个问题,他最近想了解和学习大附件上传下载的相关技术,他也是刚开始学习和接触这一块,对很多方面都不是太了解,我先花了1个小时左右的时间先给他讲解了一下基础知识,技术原理,又花了一个小时的时间帮他安装开发环境,主要就是安装开发工具和配置环境,由于在百度上面找到的很多开发工具有问题,有的不能安装,有
避开PSRR仿真三大坑:用Cadence psspxf分析分频器时,这些设置错了白忙活
本文深入探讨了使用Cadence psspxf工具进行分频器PSRR仿真时常见的三大技术陷阱,包括扰动信号参数设置、收敛性优化和后处理信号提取。通过详细的参数推荐和实战技巧,帮助工程师避免仿真误差,提升PSRR分析效率与准确性,特别适用于16分频器等数字-模拟混合电路设计。
深入解析Flex NOC总线驱动的实时带宽与延迟优化策略
本文深入解析了Flex NOC总线驱动的核心价值与优化策略。通过解读`/proc/noc/real_time`中的实时带宽与延迟数据,并结合`sysfs`接口进行动态QoS调优,可以有效解决芯片内部数据拥堵问题。文章以智能摄像头和算力盒子的实际案例,详细说明了如何通过优先级调整、带宽限制和参数优化,提升系统在复杂任务下的实时性与稳定性。
如何向服务器上传文件
可以安装xftp软件,在传输协议那边有两个可以选择,一个是ftp,另一个是sftp协议,其中一个连接服务器不成功,就换一个模式,继续连接。
Open3D DBSCAN聚类
DBSCAN算法做为一种有名的密度聚类算法,它是通过邻域参数(Distance,MinPts)来刻画样本分布的紧密程度。而在真正开始之前,首先要理解下面这几个概念:更为具体的内容也可参考这篇文章点云DBSCAN聚类算法。...
QT创建线程-基于QThread的简单封装
基于QThread类的线程类简单封装,便于反复执行线程逻辑代码与退出。
(11)(2.2.2) BLHeli32,AM32, and BLHeli_S ESCs(一)
Ardupilot官网关于《Peripheral Hardware》的翻译 —《(2.2.2) BLHeli32,AM32, and BLHeli_S ESCs》。
ICC2:使用analyze_lib_cell_placement检查lib cell的pass rate
我正在「拾陆楼」和朋友们讨论有趣的话题,你⼀起来吧?在place之前需要使analyze_lib_cell_placement命令去检查lib cell在当前的site row/power plan/legalize设置的环境下能够正常place和legalize。下面分享一个脚本去报告pass rate低于2%的lib cell。
文件上传本地服务器
文件上传的本质就是将文件通过IO流复制到服务器。文件上传是先上传到临时目录,然后再从临时目录复制到磁盘,目的是防止用户在上传途中网路中断、刷新页面导致文件上传失败,从而磁盘中产生大量垃圾文件和正常文件混在一起,所以只有文件上传成功后,才会从临时文件复制到磁盘,之后系统会自动删除临时文件。
STM32开发 -- GPS模块开发详解
一、了解硬件 我使用的GPS模块型号为UBX-M8030 参看:UBX-M8030 系列 查看一下它的特性: 多用途 GNSS 芯片,提供三种产品等级 最多可并发接收 3 个 GNSS(GPS、伽利略、GLONASS、北斗) 行业领先的 -167 dBm 导航灵敏度 业界最低电流消耗 在城市峡谷中具有绝佳的定位精度 安全性和完整性保护 支持所有的卫星增强系统 车载级芯片的工作...
利用WebClient类向服务器上载文件(转孟子的)
利用WebClient类向服务器上载文件(转孟子的) 今天看了TERRYLEE老大的一篇写上传的.里面有个WEBCLIENT我就上网查了一下,找到孟子大人的一篇文章.感觉不错,就转了过来.大家一起学习下..NET 提供了许多上载文件的方法,在Windows Form应用程序中,我们可以使用WebClient类来实现。WebClient类也有两个方法可以上载,UploadFile和...
WINCC7.3破解文件
WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!
贝叶斯统计 [韦来生编]
贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。 本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。 大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。 作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。 本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

2763



被折叠的 条评论
为什么被折叠?



