根据网络地址下载文件

private void down()
{
 //本地文件夹
    string folder = @"C:\Commodity Search";
    string contact_list = @"http://intranet.hp.com/cass/procurementcentral/ApprovedSupplierCompliance/Documents/Contact%20List.xlsx";
    string print = @"http://intranet.hp.com/cass/procurementcentral/ApprovedSupplierCompliance/Documents/Print%20Commodity%20Code%20Contact%20List.xlsx";
    string Travel = @"http://intranet.hp.com/cass/procurementcentral/ApprovedSupplierCompliance/Documents/Travel%20Contact%20List.xlsx";
    string CommodityMasterDataWorksheet = @"http://teams1.sharepoint.hp.com/teams/ASLLopez/Shared%20Documents/Master%20Data/CommodityMasterDataWorksheet.xls";
    string Approved_Supplier_Library = @"http://intranet.hp.com/cass/procurementcentral/ApprovedSupplierCompliance/Documents/Approved%20Supplier%20Library.xlsm";
    this.tsStatus.Text = "Downloading...";

    this.tsStatusNum.Text = "1/5";
    DownExcel(contact_list, folder + @"\Contact List.xlsx");

    this.tsStatusNum.Text = "2/5";
    DownExcel(print, folder + @"\Print Commodity Code Contact List.xlsx");

    this.tsStatusNum.Text = "3/5";
    DownExcel(Travel, folder + @"\Travel Contact List.xlsx");

    this.tsStatusNum.Text = "4/5";
    DownExcel(CommodityMasterDataWorksheet, folder + @"\CommodityMasterDataWorksheet.xls");

    this.tsStatusNum.Text = "5/5";
    DownExcel(Approved_Supplier_Library, folder + @"\Approved Supplier Library.xlsm");

    this.tsStatus.Text = "Completed";
    this.tsStatusNum.Text = "";

    MessageBox.Show("文件下载完成!");

}

public void DownExcel(string url, string filepath)
{
    try
    {
        //string localFilePath = "http://enhanced1.sharepoint.hp.com/teams/Quote/NBQ/Shared%20Documents/TCO_Comment_List.xls";
        string localFilePath = url;
        // string downloadpath = Application.StartupPath + @"\TEMP\comment.xls";
        string downloadpath = filepath;
        //DirectoryInfo TheFolder = new DirectoryInfo(System.Windows.Forms.Application.StartupPath + @"\TEMP\");
        DirectoryInfo TheFolder = new DirectoryInfo(@"C:\Commodity Search");
        if (TheFolder.Exists)
        {
            if (File.Exists(downloadpath))
            {
                File.Delete(downloadpath);
            }
        }
        else
        {
            TheFolder.Create();
        }
       
        System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(localFilePath);
        Myrq.Credentials = CredentialCache.DefaultCredentials;
        System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
        System.IO.Stream st = myrp.GetResponseStream();

        System.IO.Stream so = new System.IO.FileStream(downloadpath, System.IO.FileMode.Create);
        byte[] by = new byte[1024];
        int osize = st.Read(by, 0, (int)by.Length);
        while (osize > 0)
        {
            so.Write(by, 0, osize);
            osize = st.Read(by, 0, (int)by.Length);
        }

        so.Close();
        st.Close();
        myrp.Close();
        Myrq.Abort();
    }
    catch (System.Exception ex)
    {
        MessageBox.Show(ex.Message);
        //MessageBox.Show("Download file failed! Err!" + ex.StackTrace);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值