zoukankan      html  css  js  c++  java
  • C#中使用Response下载

    正常流程

    正常的从服务器端下载文件的流程
    System.IO.FileInfo file = new System.IO.FileInfo(s_path);
    HttpContext.Current.Response.ContentType = "application/ms-download";
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
    HttpContext.Current.Response.Charset = "utf-8";
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
    HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
    HttpContext.Current.Response.WriteFile(file.FullName);
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.End();

    调试

    对于最后一句代码HttpContext.Current.Response.End();
    此语句执行后 后面的代码就不会被执行。
    查看Log日志中记录的Exception的信息,显示HttpContext.Current.Response.End()这条语句正在停止进程,也就是不会再执行后面的代码。

    总结

    如果在调用该方法后还有要执行的代码,就注释掉最后一句代码。
    即为:
    System.IO.FileInfo file = new System.IO.FileInfo(s_path);
    HttpContext.Current.Response.ContentType = "application/ms-download";
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
    HttpContext.Current.Response.Charset = "utf-8";
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
    HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
    HttpContext.Current.Response.WriteFile(file.FullName);
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.Clear();
    //注释该行代码
    //HttpContext.Current.Response.End();
  • 相关阅读:
    emacs 探索之六:latex中文支持
    One网络模拟器探索之六:Report类的扩展
    emacs 探索之五:latex配置
    emacs 探索之三:基本操作
    DataSet数据传送性能比较
    SQL 2008 附加数据库报5120的错误的解决办法
    软件工程师不可不知的10个概念
    在日期上加上相应天数,并在GridView上显示
    SQL 跨表更新
    SQLSERVER 处理两个日期相减
  • 原文地址:https://www.cnblogs.com/masonblog/p/12740743.html
Copyright © 2011-2022 走看看