利用WebClient实现文件传送

来源:互联网 发布:美工专业就业前景 编辑:程序博客网 时间:2024/04/30 20:49

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();        }    }}