asp.net下载文件几种方式总结

简介:

asp.net下载的几种常用的方式如下。在下载的时候,为了防止弹出窗口,可以采用在原页面加一个

<iframe id="iDownload" style="display:none;" ></iframe>

然后用jquery语句指定下载链接。

$("#iDownload").attr("src", "download.ashx?filename=" + encodeURI($(this).children("span").first().text()));

 

protected void Button1_Click(object sender, EventArgs e) 
  { 
  /* 
  微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite 
  下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。 
  代码如下: 
  */ 
  Response.ContentType = "application/x-zip-compressed"; 
  Response.AddHeader("Content-Disposition", "attachment;filename=z.zip"); 
  string filename = Server.MapPath("DownLoad/aaa.zip"); 
  Response.TransmitFile(filename); 
  }


  //WriteFile实现下载 
  protected void Button2_Click(object sender, EventArgs e) 
  { 
  /* 
  using System.IO; 
  */ 
  string fileName ="aaa.zip";//客户端保存的文件名 
  string filePath=Server.MapPath("DownLoad/aaa.zip");//路径 
  FileInfo fileInfo = new FileInfo(filePath); 
  Response.Clear(); 
  Response.ClearContent(); 
  Response.ClearHeaders(); 
  Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); 
  Response.AddHeader("Content-Length", fileInfo.Length.ToString()); 
  Response.AddHeader("Content-Transfer-Encoding", "binary"); 
  Response.ContentType = "application/octet-stream"; 
  Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); 
  Response.WriteFile(fileInfo.FullName); 
  Response.Flush(); 
  Response.End(); 
  }


  //WriteFile分块下载 
  protected void Button3_Click(object sender, EventArgs e) 
  { 
  string fileName = "aaa.zip";//客户端保存的文件名 
  string filePath = Server.MapPath("DownLoad/aaa.zip");//路径 
  System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); 
  if (fileInfo.Exists == true) 
  { 
  const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力 
  byte[] buffer = new byte[ChunkSize]; 
  Response.Clear(); 
  System.IO.FileStream iStream = System.IO.File.OpenRead(filePath); 
  long dataLengthToRead = iStream.Length;//获取下载的文件总大小 
  Response.ContentType = "application/octet-stream"; 
  Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName)); 
  while (dataLengthToRead &gt; 0 && Response.IsClientConnected)  
  { 
  int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小 
  Response.OutputStream.Write(buffer, 0, lengthRead); 
  Response.Flush(); 
  dataLengthToRead = dataLengthToRead - lengthRead; 
  } 
  Response.Close(); 
  } 
  }


  //流方式下载 
  protected void Button4_Click(object sender, EventArgs e) 
  { 
  string fileName = "aaa.zip";//客户端保存的文件名 
  string filePath = Server.MapPath("DownLoad/aaa.zip");//路径 
  //以字符流的形式下载文件 
  FileStream fs = new FileStream(filePath, FileMode.Open); 
  byte[] bytes = new byte[(int)fs.Length]; 
  fs.Read(bytes, 0, bytes.Length); 
  fs.Close(); 
  Response.ContentType = "application/octet-stream"; 
  //通知浏览器下载文件而不是打开 
  Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); 
  Response.BinaryWrite(bytes); 
  Response.Flush(); 
  Response.End(); 
  } 














本文转自cnn23711151CTO博客,原文链接: http://blog.51cto.com/cnn237111/699359,如需转载请自行联系原作者

相关文章
|
4月前
|
C++ Windows
.NET Framework安装不成功,下载`NET Framework 3.5`文件,Microsoft Visual C++
.NET Framework常见问题及解决方案汇总,涵盖缺失组件、安装失败、错误代码等,提供多种修复方法,包括全能王DLL修复工具、微软官方运行库及命令行安装等,适用于Windows系统,解决应用程序无法运行问题。
333 3
|
1月前
|
开发框架 安全 .NET
Microsoft .NET Framework 3.5、4.5.2、4.8.1,适用于 Windows 版本的 .NET,Microsoft C Runtime等下载
.NET Framework是Windows平台的开发框架,包含CLR和FCL,支持多种语言开发桌面、Web应用。常用版本有3.5、4.5.2、4.8.1,系统可同时安装多个版本,确保软件兼容运行。
510 0
Microsoft .NET Framework 3.5、4.5.2、4.8.1,适用于 Windows 版本的 .NET,Microsoft C Runtime等下载
|
3月前
.NET Framework 3.5离线安装包合集下载
本文介绍了如何获取和安装.NET Framework运行库离线合集包。用户可通过提供的链接下载安装包,安装过程简单,按提示逐步操作即可完成。安装时可选择所需版本,工具会自动适配架构,无需手动判断,方便高效。
1445 0
|
5月前
|
存储 缓存
.NET 6中Startup.cs文件注入本地缓存策略与服务生命周期管理实践:AddTransient, AddScoped, AddSingleton。
记住,选择正确的服务生命周期并妥善管理它们是至关重要的,因为它们直接影响你的应用程序的性能和行为。就像一个成功的建筑工地,工具箱如果整理得当,工具选择和使用得当,工地的整体效率将会大大提高。
218 0
|
8月前
|
存储 XML 开发工具
【Azure Storage Account】利用App Service作为反向代理, 并使用.NET Storage Account SDK实现上传/下载操作
本文介绍了如何在Azure上使用App Service作为反向代理,以自定义域名访问Storage Account。主要内容包括: 1. **设置反向代理**:通过配置`applicationhost.xdt`和`web.config`文件,启用IIS代理功能并设置重写规则。 2. **验证访问**:测试原生URL和自定义域名的访问效果,确保两者均可正常访问Storage Account。 3. **.NET SDK连接**:使用共享访问签名(SAS URL)初始化BlobServiceClient对象,实现通过自定义域名访问存储服务。
138 1
|
11月前
|
Linux C# Windows
.NET使用MiniWord高效生成Word文件
.NET使用MiniWord高效生成Word文件
341 12
Visual Studio 快速分析 .NET Dump 文件
【11月更文挑战第10天】.NET Dump 文件是在 .NET 应用程序崩溃或出现问题时生成的,记录了应用程序的状态,包括内存对象、线程栈和模块信息。通过分析这些文件,开发人员可以定位和解决内存泄漏、死锁等问题。在 Visual Studio 中,可以通过调试工具、内存分析工具和符号加载等功能来详细分析 Dump 文件。此外,还可以使用第三方工具如 WinDbg 进行更深入的分析。
662 1
|
存储 开发框架 .NET
Windows IIS中asp的global.asa全局配置文件使用说明
Windows IIS中asp的global.asa全局配置文件使用说明
257 1
.NET 压缩/解压文件
【9月更文挑战第5天】在 .NET 中,可利用 `System.IO.Compression` 命名空间进行文件的压缩与解压。首先引入相关命名空间,然后通过 GZipStream 类实现具体的压缩或解压功能。下面提供了压缩与解压文件的方法示例及调用方式,便于用户快速上手操作。
170 6
|
C# Windows
一款基于.NET开发的简易高效的文件转换器
一款基于.NET开发的简易高效的文件转换器
120 0

热门文章

最新文章