zoukankan      html  css  js  c++  java
  • Asp.net 导出 .html,.txt 等格式的文件

          Asp.net 导出 excel 只要设置 Response 的 ContentType 为 excel 所要求的 ContentType 就行了,导出来的 excel 可以给用户下载保存。但是怎样导出 .html 等格式的文件呢?我将 Response 的 ContentType 设置成了 html 的 ContentType,但是点导出之后,生成的文件会被浏览器直接打开。难道 web 程序不能导出 .html 文件让用户下载?答案是否定的。我在用 Gmail 的时候就上传过 html 的附件,并且可以把附件的 html 文件下载下来。那就可以肯定 web 程序导出 html 文件是可以实现的!
           实现的方法其实很简单,不过需要使用一点小技巧。

    StringWriter sw = new StringWriter(); 
    sw.WriteLine(
    "html export test");
    sw.Close(); 
    Response.Clear();
    Response.AddHeader(
    "Content-Disposition""attachment; filename=test.html"); 
    Response.ContentType 
    = "application/ms-excel";//定义成 excel 需要让用户自己下载
    Response.ContentEncoding = Encoding.GetEncoding("gb2312");
    Response.Write(sw); 
    Response.End(); 

           以上实现方式参考了博客园 LoveCherry 的文章。具体地址链接一下找不到了。
  • 相关阅读:
    Android按钮事件的4种写法
    VB.NET转C#代码的工具
    C# FTP操作类
    Linq一对多联合查询
    软件工程师面试题(一)
    一道网传上海幼儿园升小学的数学题
    csdn博客刷点击率(java代码)
    .NET高端职位招聘要求
    csdn博客刷粉代码
    jQuery Ajax无刷新操作
  • 原文地址:https://www.cnblogs.com/focus/p/1053346.html
Copyright © 2011-2022 走看看