利用WebClient实现文件传送
2013-08-15 20:37
281 查看
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; //添加命名引用空间 using System.Net; using System.IO; using System.Threading; namespace 利用WebClient实现文件传送 { public partial class Form1 : Form { public Form1() { InitializeComponent(); //取消此异常以便跨线程访问控件 CheckForIllegalCrossThreadCalls = false; } private void StartDownload() { buttonDownLoad.Enabled = false; string URI = textBoxURI.Text; int n = URI.LastIndexOf('/'); string URIAddress = URI.Substring(0, n); string fileName = URI.Substring(n + 1, URI.Length - n - 1); string Dir = textBoxSavePath.Text; string path = Dir + '\\' + fileName; try { WebRequest myre = WebRequest.Create(URIAddress); } catch (WebException ex) { MessageBox.Show(ex.Message, "创建URI错误"); } WebClient DownLoadClient = new WebClient(); try { statusStrip1.Items[0].Text = "开始下载文件……"; //将具有指定URI的资源下载到本地文件 DownLoadClient.DownloadFile(URI, path); statusStrip1.Items[0].Text = "下载完成。"; } catch (WebException ex) { MessageBox.Show(ex.Message, "下载错误"); statusStrip1.Items[0].Text = ""; } buttonDownLoad.Enabled = true; } private void buttonDownLoad_Click(object sender, EventArgs e) { if (textBoxSavePath.Text == "") { MessageBox.Show("请选择保存路径后再下载"); return; } Thread thDownLoad = new Thread(new ThreadStart(StartDownload)); thDownLoad.Start(); } //选择下载保存的文件夹 private void buttonFind1_Click(object sender, EventArgs e) { SaveFileDialog dlgSaveFile = new SaveFileDialog(); if (dlgSaveFile.ShowDialog() == DialogResult.OK) { string sFileInfo = dlgSaveFile.FileName; textBoxSavePath.Text = sFileInfo.Substring(0, sFileInfo.LastIndexOf("\\")); statusStrip1.Items[0].Text = "就绪"; } } //上传文件 private void StartUpLoad() { if (textBoxUpload.Text.Trim() == "" || textBoxURI.Text.Trim() == "") { MessageBox.Show("请输入要上载的文件名。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { string filenamepath = textBoxUpload.Text.Trim(); string uristring = textBoxURI.Text.Trim(); string filename = filenamepath.Substring(filenamepath.LastIndexOf("\\") + 1); if(uristring.EndsWith("/")==false) { uristring = uristring + "/"; } uristring = uristring + filename; //创建WebClient实例 WebClient UpLoadClient = new WebClient(); //获取与FTP服务器通信的凭据 UpLoadClient.Credentials = CredentialCache.DefaultCredentials; //以文件流方式上传文件 FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); //采用二进制数据流 BinaryReader r = new BinaryReader(fs); try { byte[] postarray = r.ReadBytes((int)fs.Length); //打开一个文件进行些操作 Stream poststream = UpLoadClient.OpenWrite(uristring, "put"); if (poststream.CanWrite) { poststream.Write(postarray, 0, postarray.Length); statusStrip1.Items[0].Text = filename + "上传成功"; } else { statusStrip1.Items[0].Text = "文件目前不可写"; } poststream.Close(); } catch (WebException ex) { statusStrip1.Items[0].Text = "上传失败" + ex.Message; } } } private void buttonFind_Click(object sender, 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) { textBoxUpload.Text = dlgOpenFile.FileName.ToString(); statusStrip1.Items[0].Text = "就绪"; } } } private void buttonUpload_Click(object sender, EventArgs e) { Thread thUpload = new Thread(new ThreadStart(StartUpLoad)); thUpload.Start(); } } }
相关文章推荐
- C#利用WebClient实现两种方式下载文件
- 利用Flex实现P2P文件传送
- Android上利用SDK的BluetoothAPI实现传送文件
- 利用Flex实现P2P文件传送
- Android上利用SDK的BluetoothAPI实现传送文件
- 利用Flex实现P2P文件传送
- iOS-利用AFNetworking(AFN 1.x)-实现文件断点下载
- php利用iframe实现无刷新文件上传功能
- php 利用阿里云oss实现托管文件
- 利用VC++实现AVI文件的合成和分解(Bmp图片)
- 利用WebUploader实现大文件上传和视频上传
- 利用xshell及rz,sz实现拖曳上传文件到Linux服务器
- 利用xshell及rz,sz实现拖曳上传文件到Linux服务器
- Framework7中利用download和external实现a标签下载文件和图片
- 琐记9:利用标准i/o实现文件间的运算
- 利用文件锁,实现单一线程运行
- 利用ASP.NET服务器端自定义控件实现XML文件中还原表单
- Android中利用ZipEntry漏洞实现免root写恶意文件到应用的沙盒中
- 利用FTP,实现Linux与windows文件互传
- 利用WebClient上传参数及文件流到远程ashx服务