zoukankan      html  css  js  c++  java
  • TransmitFile下载文件(部分转载)

    例子代码:

    public void Down()
    {
    TransmitFile(@"/File/KBPub.zip");
    }
    public void TransmitFile(string filePath) //filePath 下载的文件的相对路径
    {
    try
    {
    filePath = Server.MapPath(filePath);
    if (System.IO.File.Exists(filePath))
    {
    FileInfo info = new FileInfo(filePath);
    long fileSize = info.Length;
    System.Web.HttpContext.Current.Response.Clear();
    System.Web.HttpContext.Current.Response.ContentType = "application/x-zip-compressed";
    System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filePath.Substring(filePath.LastIndexOf("\\") + 1)); //filename 下载后显示的文件名
    //不指明Content-Length用Flush的话不会显示下载进度的
    System.Web.HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
    System.Web.HttpContext.Current.Response.TransmitFile(filePath, 0, fileSize);
    System.Web.HttpContext.Current.Response.Flush();
    }
    }
    catch
    { }
    finally
    {
    System.Web.HttpContext.Current.Response.Close();
    }

    }

    WriteFile方式:

    public void WriteFile(string filePath)
    {
    try
    {
    filePath = Server.MapPath(filePath);
    if (System.IO.File.Exists(filePath))
    {
    FileInfo info = new FileInfo(filePath);
    long fileSize = info.Length;
    System.Web.HttpContext.Current.Response.Clear();
    System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
    System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachement;filename=" + Server.UrlEncode(info.FullName));
    //指定文件大小
    System.Web.HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
    System.Web.HttpContext.Current.Response.WriteFile(filePath, 0, fileSize);
    System.Web.HttpContext.Current.Response.Flush();
    }
    }
    catch
    { }
    finally
    {
    System.Web.HttpContext.Current.Response.Close();
    }
    }

  • 相关阅读:
    一步一步学Remoting之四:承载方式(2)<转>
    一步一步学Remoting之五:异步操作<转>
    NET3.0+中使软件发出声音[整理篇]<转>
    Ext Core手册
    一步一步学Remoting之一:从简单开始<转>
    asp.net 常用字符串过滤方法 <转>
    mssql性能优化<转>
    一步一步学Remoting之四:承载方式(1)<转>
    Javascript中最常用的61个经典技巧 <转>
    Js事件对象<转>
  • 原文地址:https://www.cnblogs.com/jinghuimin/p/5000808.html
Copyright © 2011-2022 走看看