学习啦 > 知识大全 > 方法百科 > 方法大全 > asp.net文件的方法

asp.net文件的方法

时间: 陈哲凡673 分享

asp.net文件的方法

学习啦在线学习网   ASP 是一项微软公司的技术,是一种使嵌入网页中的脚本,可由因特网服务器执行的服务器端脚本技术。下面是学习啦小编为大家带来了一篇asp.net下载文件的方法,希望对大家有所帮助。

  asp.net下载文件的方法(一)

学习啦在线学习网   public class FileDown

  {

学习啦在线学习网   public FileDown()

  { }

学习啦在线学习网   /// <summary>

学习啦在线学习网   /// 参数为虚拟路径

  /// </summary>

学习啦在线学习网   public static string FileNameExtension(string FileName)

  {

  return Path.GetExtension(MapPathFile(FileName));

  }

  /// <summary>

  /// asp.net下载文件的方法:获取物理地址

  /// </summary>

学习啦在线学习网   public static string MapPathFile(string FileName)

  {

学习啦在线学习网   return HttpContext.Current.Server.MapPath(FileName);

  }

  /// <summary>

  /// 普通下载

学习啦在线学习网   /// </summary>

  /// <param name="FileName">文件虚拟路径</param>

  public static void DownLoadold(string FileName)

  {

  string destFileName = MapPathFile(FileName);

学习啦在线学习网   if (File.Exists(destFileName))

  {

  FileInfo fi = new FileInfo(destFileName);

  HttpContext.Current.Response.Clear();

  HttpContext.Current.Response.ClearHeaders();

  HttpContext.Current.Response.Buffer = false;

  HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(destFileName), System.Text.Encoding.UTF8));

  HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());

  HttpContext.Current.Response.ContentType = "application/octet-stream";

学习啦在线学习网   HttpContext.Current.Response.WriteFile(destFileName);

学习啦在线学习网   HttpContext.Current.Response.Flush();

学习啦在线学习网   HttpContext.Current.Response.End();

  }

  }

  /// <summary>

学习啦在线学习网   /// asp.net分块下载

学习啦在线学习网   /// </summary>

学习啦在线学习网   /// <param name="FileName">文件虚拟路径</param>

学习啦在线学习网   public static void DownLoad(string FileName)

  {

  string filePath = MapPathFile(FileName);

  long chunkSize = 204800; //指定块大小

学习啦在线学习网   byte[] buffer = new byte[chunkSize]; //建立一个200K的缓冲区

学习啦在线学习网   long dataToRead = 0; //已读的字节数

  FileStream stream = null;

  try

  {

  //打开文件

学习啦在线学习网   stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

  dataToRead = stream.Length;

  //添加Http头

学习啦在线学习网   HttpContext.Current.Response.ContentType = "application/octet-stream";

  HttpContext.Current.Response.AddHeader("Content-Disposition", "attachement;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath)));

学习啦在线学习网   HttpContext.Current.Response.AddHeader("Content-Length", dataToRead.ToString());

学习啦在线学习网   while (dataToRead > 0)

  {

学习啦在线学习网   if (HttpContext.Current.Response.IsClientConnected)

  {

  int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));

  HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);

  HttpContext.Current.Response.Flush();

学习啦在线学习网   HttpContext.Current.Response.Clear();

学习啦在线学习网   dataToRead -= length;

  }

  else

  {

学习啦在线学习网   dataToRead = -1; //防止client失去连接

  }

  }

  }

  catch (Exception ex)

  {

学习啦在线学习网   HttpContext.Current.Response.Write("Error:" + ex.Message);

  }

  finally

  {

  if (stream != null) stream.Close();

学习啦在线学习网   HttpContext.Current.Response.Close();

  }

  }

学习啦在线学习网   /// <summary>

  /// 输出硬盘文件,提供下载 支持大文件、续传、速度限制、资源占用小

  /// </summary>

学习啦在线学习网   /// <param name="_Request">Page.Request对象</param>

  /// <param name="_Response">Page.Response对象</param>

学习啦在线学习网   /// <param name="_fileName">下载文件名</param>

学习啦在线学习网   /// <param name="_fullPath">带文件名下载路径</param>

学习啦在线学习网   /// <param name="_speed">每秒允许下载的字节数</param>

  /// <returns>返回是否成功</returns>

学习啦在线学习网   //---------------------------------------------------------------------

  //调用:

学习啦在线学习网   // string FullPath=Server.MapPath("count.txt");

  // ResponseFile(this.Request,this.Response,"count.txt",FullPath,100);

  //---------------------------------------------------------------------

  public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)

  {

  try

  {

学习啦在线学习网   FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

  BinaryReader br = new BinaryReader(myFile);

  try

  {

  _Response.AddHeader("Accept-Ranges", "bytes");

  _Response.Buffer = false;

学习啦在线学习网   long fileLength = myFile.Length;

学习啦在线学习网   long startBytes = 0;

学习啦在线学习网   int pack = 10240; //10K bytes

  int sleep = (int)Math.Floor((double)(1000 * pack / _speed)) + 1;

  if (_Request.Headers["Range"] != null)

  {

  _Response.StatusCode = 206;

  string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });

学习啦在线学习网   startBytes = Convert.ToInt64(range[1]);

  }

  _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());

学习啦在线学习网   if (startBytes != 0)

  {

学习啦在线学习网   _Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));

  }

学习啦在线学习网   _Response.AddHeader("Connection", "Keep-Alive");

学习啦在线学习网   _Response.ContentType = "application/octet-stream";

  _Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));

学习啦在线学习网   br.BaseStream.Seek(startBytes, SeekOrigin.Begin);

  int maxCount = (int)Math.Floor((double)((fileLength - startBytes) / pack)) + 1;

  for (int i = 0; i < maxCount; i++)

  {

  if (_Response.IsClientConnected)

  {

  _Response.BinaryWrite(br.ReadBytes(pack));

  Thread.Sleep(sleep);

  }

  else

  {

  i = maxCount;

  }

  }

  }

  catch

  {

  return false;

  }

  finally

  {

学习啦在线学习网   br.Close();

  myFile.Close();

  }

  }

  catch

  {

  return false;

  }

  return true;

  }

  }

asp.net下载文件的方法(二)

  1 string fileName = "aaa.zip";//客户端保存的文件名

  2 string filePath = Server.MapPath("DownLoad/aaa.zip");//路径

  3

学习啦在线学习网   4 System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

  5

  6 if (fileInfo.Exists == true)

  7 {

  8 const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力

  9 byte[] buffer = new byte[ChunkSize];

  10

学习啦在线学习网   11 Response.Clear();

  12 System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);

学习啦在线学习网   13 long dataLengthToRead = iStream.Length;//获取下载的文件总大小

学习啦在线学习网   14 Response.ContentType = "application/octet-stream";

  15 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));

  16 while (dataLengthToRead > 0 && Response.IsClientConnected)

  17 {

  18 int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小

学习啦在线学习网   19 Response.OutputStream.Write(buffer, 0, lengthRead);

  20 Response.Flush();

  21 dataLengthToRead = dataLengthToRead - lengthRead;

  22 }

  23 Response.Close();

  24 }

377282