• 一个ASP.NET文件下载类


    public static class FilesDownLoad
        {
            private static readonly Dictionary<string, string> MimeDic = new Dictionary<string, string>();

            static FilesDownLoad()
            {
                MimeDic.Add("text/plain", "txt");
                MimeDic.Add("image/jpeg", "jpg");
                MimeDic.Add("image/gif", "gif");
            }

            /// <summary>
            /// 下载文件到指定目录,并返回下载后存放的文件路径
            /// </summary>
            /// <param>网址</param>
            /// <param>存放目录,如果该目录中已存在与待下载文件同名的文件,那么将自动重命名</param>
            /// <returns>下载文件存放的文件路径</returns>
            public static string DownLoadFile(Uri Uri, string savePath)
            {

                WebResponse q = WebRequest.Create(Uri).GetResponse();
                Stream s = q.GetResponseStream();
                var b = new BinaryReader(s);
                string file = CreateSavePath(savePath, Uri, q.ContentType);
                var fs = new FileStream(file, FileMode.Create, FileAccess.Write);
                fs.Write(b.ReadBytes((int)q.ContentLength), 0, (int)q.ContentLength);
                fs.Close();
                b.Close();
                s.Close();
                return file;
            }

            private static string CreateSavePath(string SaveCategory, Uri Uri, string ContentType)
            {
                if (Directory.Exists(SaveCategory) == false)
                    Directory.CreateDirectory(SaveCategory);

                string ex = GetExtentName(ContentType);
                string up = null;
                string upne = null;
                if (Uri.LocalPath == "/")
                {
                    //处理Url是域名的情况
                    up = upne = Uri.Host;
                }

                else
                {
                    if (Uri.LocalPath.EndsWith("/"))
                    {
                        //处理Url是目录的情况
                        up = Uri.LocalPath.Substring(0, Uri.LocalPath.Length - 1);
                        upne = Path.GetFileName(up);
                    }

                    else
                    {
                        //处理常规Url

                        up = Uri.LocalPath;
                        upne = Path.GetFileNameWithoutExtension(up);
                    }
                }

                string name = string.IsNullOrEmpty(ex) ? Path.GetFileName(up) : upne + "." + ex;
                string fn = Path.Combine(SaveCategory, name);
                int x = 1;

                while (File.Exists(fn))
                {
                    fn = Path.Combine(SaveCategory,
                                      Path.GetFileNameWithoutExtension(name) + "(" + x++ + ")" + Path.GetExtension(name));
                }

                return fn;
            }

            private static string GetExtentName(string ContentType)
            {
                foreach (string f in MimeDic.Keys)
                {
                    if (ContentType.ToLower().IndexOf(f) >= 0) return MimeDic[f];
                }

                return null;
            }
        }

     

    测试用法如下:

    [TestFixture]
    public class FileDownLoadTests
    {
        [Test]
        [Ignore]
        public void 测试下载()
        {
            string d = @"D:\MyTest\Downloads";
            //首次下载

            Assert.AreEqual(@"D:\MyTest\Downloads\cf697a340f684bc1a9018e98.jpg",
                            FilesDownLoad.DownLoadFile(new Uri("http://hiphotos.baidu.com/huyangdiy/pic/item/cf697a340f684bc1a9018e98.jpg"), d));

            //第二次下载,遇到同名文件,自动重命名

            Assert.AreEqual(@"D:\MyTest\Downloads\cf697a340f684bc1a9018e98(1).jpg",
                            FilesDownLoad.DownLoadFile(new Uri("http://hiphotos.baidu.com/huyangdiy/pic/item/cf697a340f684bc1a9018e98.jpg"), d));

        }
    }

  • 相关阅读:
    各项硬件使用剖析(一)---让你一眼就能区分瓶颈是Memory、processor ORdisk!
    基本算术运算符
    网页计算器 && 简易网页时钟 && 倒计时时钟
    页面加载后累加,自加1&&判断数字是否为两位数
    累加按钮,自加1&&输入两个数字,比较大小
    用typeof查看数据类型&&用parseInt解析数字,并求和
    鼠标移过,改变图片路径
    单一按钮显示/隐藏&&提示框效果
    简易选项卡&&简易JS年历
    函数传参,改变Div任意属性的值&&图片列表:鼠标移入/移出改变图片透明度
  • 原文地址:https://www.cnblogs.com/yuananyun/p/2037570.html
Copyright © 2020-2023  润新知