如何用ASP.NET直接下载文件

codecb 2004-06-10 07:47:31

比如点下载就可以下载文件??
...全文
3789 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
gOODiDEA 2004-06-10
  • 打赏
  • 举报
回复
System.IO.FileStream fs = System.IO.File.OpenRead( "C:\123.txt" );
byte[] FileData = new byte[ fs.Length ];
fs.Read( FileData, 0, ( int ) fs.Length );
Response.Clear();
Response.AddHeader( "Content-Type", "application/zip" );
string FileName = System.Web.HttpUtility.UrlEncode( System.Text.Encoding.UTF8.GetBytes( "测试") );
Response.AddHeader("Content-Disposition", "inline;filename="+ System.Convert.ToChar(34) + FileName + System.Convert.ToChar(34) );
Response.AddHeader("Content-Length", fs.Length.ToString() );
Response.BinaryWrite( FileData );
fs.Close();
codecb 2004-06-10
  • 打赏
  • 举报
回复

搞好了 给分
devfan 2004-06-10
  • 打赏
  • 举报
回复
pierven(牛牛) 的会用IE直接打开文件的

codecb(阿星)的应该没有问题的

要不试试

string path =@"G:\download\down.txt";
codecb 2004-06-10
  • 打赏
  • 举报
回复
不行呀
codecb 2004-06-10
  • 打赏
  • 举报
回复
使用ASP.NET直接下载文件 发布时间:2004-5-24



文件下载

一. 服务端通过Response输出相应的HTTP Response Headers信息,和要下载的文件的数据来把文件发送到客户端,HTTP Response Headers表现在html文件中是下面的形式:
<meta http-equiv="Content-Type" content="text/htm ">
http-equiv表示是Headers的名称,content表示这个Headers的值

二. 首先,要输出文件的MIME类型:
Page.Response.AddHeader( "Content-Type", “MIME类型” );

三. 其次,要输出下载的文件的打开位置和文件名:
Page.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName );
content-disposition 的 HTTP response header 允许指定文档表示的信息。使用这种 header ,你就可以将文档指定成单独打开(而不是在浏览器中打开),还可以根据用户的操作来显示。如果用户要保存文档,你还可以为该文档建议一个文件名。这个建议名称会出现在 Save As 对话框的“文件名”栏中。
attachment ―― 表示作为附件发送到客户端,客户端将单独打开此文件。
inline ―― 表示将在浏览器中打开这个文件。
filename ―― 表示发送到客户端文件的文件名。

四. 准备发送到客户端的文件数据:
不管什么类型的文件都要先转成byte类型的数组,然后将这个byte数组用Response.BinaryWrite方法输出到客户端。

string path ="G:\\download\\down.txt";
System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name));

Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();

pierven 2004-06-10
  • 打赏
  • 举报
回复
System.Web.HttpContext myhttpcontext = System.Web.HttpContext.Current;
myhttpcontext.Response.AppendHeader("Content-Disposition","attachment;filename=" + outputpath);//outputpath为下载文件在服务器端的路径 System.IO.FileStream myfilestream = new System.IO.FileStream(inputpath,System.IO.FileMode.Open);
System.IO.BinaryReader mybinaryreader = new System.IO.BinaryReader(myfilestream);
int i;
for (i=0; i<myfilestream.Length;i++)
myhttpcontext.Response.OutputStream.WriteByte(mybinaryreader.ReadByte());
mybinaryreader.Close();
myfilestream.Close();
myhttpcontext.Response.End();

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧