DownloadDocument.cs
上传用户:huiyue
上传日期:2022-04-08
资源大小:1429k
文件大小:1k
源码类别:

搜索引擎

开发平台:

ASP/ASPX

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Searcharoo.Common
  5. {
  6.     public abstract class DownloadDocument : Document
  7.     {
  8.         public DownloadDocument(Uri location)
  9.             : base(location)
  10.         {}
  11.         protected void SaveDownloadedFile(System.Net.HttpWebResponse webresponse, string filename)
  12.         {
  13.             System.IO.Stream filestream = webresponse.GetResponseStream();
  14.             this.Uri = webresponse.ResponseUri;
  15.             using (System.IO.BinaryReader reader = new System.IO.BinaryReader(filestream))
  16.             {
  17.                 using (System.IO.FileStream iofilestream = new System.IO.FileStream(filename, System.IO.FileMode.Create))
  18.                 {
  19.                     int BUFFER_SIZE = 1024;
  20.                     byte[] buf = new byte[BUFFER_SIZE];
  21.                     int n = reader.Read(buf, 0, BUFFER_SIZE);
  22.                     while (n > 0)
  23.                     {
  24.                         iofilestream.Write(buf, 0, n);
  25.                         n = reader.Read(buf, 0, BUFFER_SIZE);
  26.                     }
  27.                     this.Uri = webresponse.ResponseUri;
  28.                     this.Length = iofilestream.Length;
  29.                     iofilestream.Close();
  30.                     iofilestream.Dispose();
  31.                 }
  32.                 reader.Close();
  33.             }
  34.         }
  35.     }
  36. }