天天看點

Web下載下傳檔案

IE浏覽器在打開swf、wma等檔案的時候會自動打開,如果想下載下傳到本地存儲就不太友善,為此我自己做一個小程式用來下載下傳檔案,

由于我在區域網路内使用代理伺服器上網,是以下載下傳檔案還需要通過代理設定。

程式截圖:

下載下傳按鈕事件的代碼:

private void buttonDownload_Click(object sender, EventArgs e)

        {

            TIMMS.DATA.TWebClient wc = new TIMMS.DATA.TWebClient();

            if (checkBox1.Checked)

            {

                wc.ProxyUserName = textBoxUsername.Text;

                wc.ProxyPassword = textBoxPassword.Text;

                wc.ProxyDomainName = textBoxDomainname.Text;

            }

            wc.ProxyUrl = textBoxUrl.Text;

            SaveFileDialog saveFileDialog = new SaveFileDialog();

            int indexof=textBoxUrl.Text.LastIndexOf("/")+1;

            int length=textBoxUrl.Text.Length;

            string filename=textBoxUrl.Text.Substring(indexof, (length - indexof));

            string file_extension = System.IO.Path.GetExtension(filename);

            saveFileDialog.FileName = filename;

            saveFileDialog.Filter = "檔案 (*" + file_extension + ")|*" + file_extension + "|所有檔案|*.*";

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

                wc.FileName = saveFileDialog.FileName;

            string errmsg="";

            Cursor.Current = Cursors.WaitCursor;

            buttonDownload.Enabled = false;

            if (!wc.GetFile(out errmsg))

                MessageBox.Show(errmsg, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                buttonDownload.Enabled = true;

                Cursor.Current = Cursors.Default;

                return;

            buttonDownload.Enabled = true;

            Cursor.Current = Cursors.Default;

            MessageBox.Show("下載下傳完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information );

        }

 TIMMS.DATA.TWebClient :

using System;

using System.Collections.Generic;

using System.Text;

using System.Net;

namespace TIMMS.DATA

{

   public  class TWebClient

    {

      /// <summary>

      /// 供向 URI 辨別的資源發送資料和從 URI 辨別的資源接收資料的公共方法

      /// </summary>

       public TWebClient() { }

       /// <summary>

       /// TWebClient構造函數

       /// </summary>

       /// <param name="user">代理伺服器使用者名</param>

       /// <param name="password">代理伺服器登入密碼</param>

       /// <param name="domainName">代理伺服器域名</param>

       /// <param name="Address">代理伺服器位址</param>

       /// <param name="url">需要下載下傳的Web檔案路徑</param>

       /// <param name="filename">儲存到本地的檔案名</param>

       public  TWebClient(string user,string password,string domainName,string Address,string url,string filename)

       {

           proxyUser=user;

           ProxyPassword=password;

           proxyDomainName=domainName;

           proxyAddress = Address;

           proxyUrl = url;

           fileName = filename;

       }

       ///  TWebClient構造函數,使用IE預設代理設定

       public  TWebClient(string user, string password, string domainName, string url, string filename)

           proxyUser = user;

           ProxyPassword = password;

           proxyDomainName = domainName;

       /// WebClient構造函數,使用IE預設代理設定

       public  TWebClient(string user, string password, string domainName)

       public TWebClient(string url, string filename)

        private  string proxyUser = "";

        /// <summary>

        /// 代理伺服器使用者名

        /// </summary>

        public  string ProxyUserName

            get { return proxyUser; }

            set { proxyUser = value; }

        private  string proxyPassword = "";

        /// 代理伺服器使用者密碼

        public  string ProxyPassword

            get { return proxyPassword; }

            set { proxyPassword = value; }

        private  string proxyDomainName = "";

        /// 代理伺服器域名

        public  string ProxyDomainName

            get { return proxyDomainName; }

            set { proxyDomainName = value; }

        private  string proxyAddress;

        /// 預設與IE設定相同

        public  string ProxyAddress

            get { return proxyAddress; }

            set { proxyAddress = value; }

        public  string ProxyUrl

            get { return proxyUrl; }

            set { proxyUrl = value; }

       private  string fileName = "";

       /// 下載下傳檔案的檔案名(包含路徑)@"d:\baidu_logo.gif";

        public  string FileName

            get { return fileName; }

            set { fileName = value; }

       /// 根據TWebClient設定擷取并儲存檔案

       /// <param name="errmsg"></param>

       /// <returns></returns>

        public  bool GetFile(out string errmsg)

            try

                WebClient wc = new WebClient();

                WebProxy proxy = WebProxy.GetDefaultProxy();//擷取IE預設設定

                //如果預設設定為空,則有可能是根本不需要代理伺服器,如果此時配置檔案中也未配置則認為不需Proxy

                if (proxy.Address == null && proxyAddress != null && proxyAddress != "")

                    proxy.Address = new Uri(proxyAddress);//按配置檔案建立Proxy 地置

                if (proxy.Address != null)//如果位址為空,則不需要代理伺服器

                {

                    proxy.Credentials = new NetworkCredential(proxyUser, proxyPassword, proxyDomainName);//從配置封裝參數中建立

                    wc.Proxy = proxy;//request.Proxy = proxy;//賦予 request.Proxy 

                }

                wc.DownloadFile(proxyUrl, fileName);//下載下傳檔案并儲存

                errmsg = "OK";

                return true;

            catch (Exception ex)

                errmsg = ex.Message;

                return false;

        ///  根據TWebClient設定擷取并儲存檔案

        /// <param name="url">需要下載下傳的Web檔案路徑</param>

        /// <param name="filename">儲存到本地的檔案名</param>

       /// <param name="errmsg">錯誤消息</param>

       /// <returns>成功true/失敗false</returns>

       public bool GetFile(string url, string filename,out string errmsg)

           return GetFile(out errmsg);

    }

}

本文轉自tiasys部落格園部落格,原文連結:http://www.cnblogs.com/tiasys/archive/2010/08/09/1795858.html,如需轉載請自行聯系原作者

繼續閱讀