zoukankan      html  css  js  c++  java
  • c# WebClient文件下载

     public void HttpDownload(string url, string path, ResourceType type)
            {
                using (var client = new WebClient())
                {
                    client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
                    client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                    var stream = client.OpenRead(url);
                    if (type == ResourceType.Document)
                    {
                        var header = client.ResponseHeaders["Content-Disposition"];
                        var subStr = header.Split('=');
                        var fileName = System.Web.HttpUtility.UrlDecode(subStr[1]);
                        path = GetFileValidation(path, fileName, out fileName);
                    }
                    FileStream writeStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                    ReadWriteStream(stream, writeStream);
                }
            }
    private void ReadWriteStream(Stream readStream, Stream writeStream)
            {
                int Length = 256;
                Byte[] buffer = new Byte[Length];
                int bytesRead = readStream.Read(buffer, 0, Length);
                // write the required bytes
                while (bytesRead > 0)
                {
                    writeStream.Write(buffer, 0, bytesRead);
                    bytesRead = readStream.Read(buffer, 0, Length);
                }
                readStream.Close();
                writeStream.Close();
            }
    //验证文件是否存在
     public string GetFileValidation(string path, string oldTitle, out string newTitle)
            {
                string newPath = System.IO.Path.Combine(path, oldTitle.Replace(""", ""));
                string title1 = oldTitle;
    
                if (File.Exists(newPath))
                {
                    for (int i = 1; i < int.MaxValue; i++)
                    {
                        newPath = System.IO.Path.Combine(path, oldTitle.Split('.')[0] + "(" + i + ")" + "." + oldTitle.Split('.')[1]);
                        if (!File.Exists(newPath))
                        {
                            title1 = oldTitle.Split('.')[0] + "(" + i + ")" + "." + oldTitle.Split('.')[1];
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
                newTitle = title1;
                return newPath;
            }
    

      

  • 相关阅读:
    客户端用mstsc不能用一台设备连接终端服务器的解决办法
    [转]知识管理ABC
    Visual Studio常用小技巧[备忘]
    一套外企的数据库设计面试题
    MSDN中的图形元素和文档约定[备忘]
    设计模式概述
    ASP.Net 4.0中新增加的23项功能[转]
    Dreamweaver 8 的相关使用
    浅谈ThreadPool 线程池
    C#委托的异步调用[学习]
  • 原文地址:https://www.cnblogs.com/zhtbk/p/5227507.html
Copyright © 2011-2022 走看看