Sun_china

交流更多,收获更多?

博客园 首页 新随笔 联系 订阅 管理
  184 随笔 :: 14 文章 :: 29 评论 :: 31万 阅读

第一种方法:
   Response.ClearContent();
  Response.ClearHeaders();
  Response.ContentType = "Application/msword";
  string s=Server.MapPath("C#语言参考.doc");
   Response.WriteFile("C#语言参考.doc");
  Response.Write(s);
  Response.Flush();
    Response.Close();
第二种方法:

   Response.ClearContent();
  
   Response.ClearHeaders();
   
   Response.ContentType   =   "Application/msword";  

   string   strFilePath="";  

   strFilePath   =Server.MapPath("C#语言参考.doc");
 
   FileStream   fs   =   new   FileStream(strFilePath,FileMode.OpenOrCreate,FileAccess.Read);
      
   Response.WriteFile(strFilePath,0,fs.Length);

   fs.Close();

第三种方法:

string path=Server.MapPath("C#语言参考.doc");

   FileInfo file=new FileInfo(path);
 
   FileStream myfileStream=new FileStream(path,FileMode.Open,FileAccess.Read);
 
   byte[] filedata=new Byte[file.Length];

   myfileStream.Read(filedata,0,(int)(file.Length));
 
   myfileStream.Close();

   Response.Clear();

   Response.ContentType="application/msword";

   Response.AddHeader("Content-Disposition","attachment;filename=文件名.doc");

   Response.Flush();

   Response.BinaryWrite(filedata);

   Response.End();

posted on   Sun_china  阅读(5733)  评论(2编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示