ASP.NET(c#)文件下载函数

  1/// <summary>
  2/// 下载文件函数
  3/// </summary>
  4/// <param name="url">下载地址</param>
  5/// <param name="Path">保存路径</param>
  6/// <param name="typeName">类型:文件或图片</param>

  7        private bool DownFile(string url, string Path,string typeName)
  8        {
  9            try
 10            {
 11                HttpWebRequest objHttpWebRequest;
 12                HttpWebResponse objHttpWebResponse;
 13                try
 14                {
 15                    objHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);//send
 16                    objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();//get
 17                    objHttpWebRequest.AllowAutoRedirect = true;
 18                }

 19                catch
 20                {
 21                    return false;
 22                }

 23
 24
 25                string AbsoluteUri = objHttpWebResponse.ResponseUri.AbsoluteUri;//It may be Redirect so is necessary to use the AbsoluteUri
 26                string Extend = AbsoluteUri.Substring(AbsoluteUri.LastIndexOf("."+ 1);
 27
 28
 29                string GuidName = Guid.NewGuid().ToString();
 30                Path = Path + GuidName.Substring(01);
 31
 32
 33                DirectoryInfo objDirectoryInfo = new DirectoryInfo(Path);
 34                if (objDirectoryInfo.Exists)
 35                {
 36
 37                }

 38                else
 39                {
 40                    objDirectoryInfo.Create();
 41                }

 42
 43
 44
 45                string FullName = Path + "\\" + GuidName + "." + Extend;
 46
 47
 48                Stream objStream = objHttpWebResponse.GetResponseStream();
 49
 50                Byte[] buffer = new Byte[256];
 51
 52                int bufferSizeCount = objStream.Read(buffer, 0256);
 53
 54                FileStream objFileStream = new FileStream(FullName, FileMode.Create, FileAccess.Write);
 55
 56                int sumBufferSizeCount = 0;
 57
 58                while (bufferSizeCount > 0)
 59                {
 60                    Application.DoEvents();
 61
 62                    objFileStream.Write(buffer, 0, bufferSizeCount);//
 63                    bufferSizeCount = objStream.Read(buffer, 0256);//
 64                    sumBufferSizeCount += bufferSizeCount;//
 65                    this.Text = string.Format("[总进度{0}/{1}]  下载进度{2}文件:{3}K  {4}",currentIndex,totalCount, Extend, sumBufferSizeCount / 1024,sumBufferSizeCount);
 66                }

 67
 68                objStream.Close();
 69                objFileStream.Close();
 70                objHttpWebResponse.Close();
 71
 72
 73                FileInfo objFileInfo = new FileInfo(FullName);
 74                int FileSize = Convert.ToInt32(objFileInfo.Length / 1024);
 75
 76                if (typeName == "image")
 77                {
 78                    ImageFileName = GuidName;
 79                    ImageExtend = Extend;
 80                    ImageFileSize = FileSize;
 81
 82                    if (File.Exists(FullName))
 83                    {
 84                        System.Drawing.Image image = System.Drawing.Image.FromFile(FullName);
 85
 86                        this.pictureBox1.Image = image;
 87                    }

 88                    
 89                }

 90                else if(typeName == "file")
 91                {
 92                    FileFileName = GuidName;
 93                    FileExtend = Extend;
 94                    FileFileSize = FileSize;
 95                }

 96
 97                this.Refresh();
 98
 99                return true;
100               
101            }

102            catch
103            {
104                return false;
105            }

106
107        }

posted on 2007-09-12 16:33  小子好黑  阅读(667)  评论(1编辑  收藏  举报

导航