马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

ASP.NET如何下载PDF文件

[复制链接]
查看7553 | 回复1 | 2013-6-6 08:37:46 | 显示全部楼层 |阅读模式
C#如何下载PDF文件

方法一:可能是最简单的、最短的方式:

Response.ContentType = "application/pdf";  

Response.AppendHeader("Content-Disposition", "attachment;

filename=MyFile.pdf");  Response.TransmitFile(Server.MapPath("~/Files/MyFile.pdf"));  

Response.End();

第一步是设置文档内容的类型,上面的例子是下载一个.PDF格式的文件。下面是最常用的一些文档内容类型:


[HTML] 纯文本查看 复制代码
.htm, .html     Response.ContentType = "text/HTML"; .txt    Response.ContentType = "text/plain";
.doc, .rtf, .docx    Response.ContentType = "Application/msword";
.xls, .xlsx    Response.ContentType = "Application/x-msexcel";
.jpg, .jpeg    Response.ContentType = "image/jpeg";
.gif    Response.ContentType =  "image/GIF";
.pdf    Response.ContentType = "application/pdf";



方法二:解决.PDF文件较大时,可能导致所下载的PDF文件无法打开的方案

本文介绍了一种在ASP.NET中下载文件的方法。
方法一:可能是最简单的、最短的方式:
  Response.ContentType = "application/pdf";
  Response.AppendHeader("Content-Disposition", "attachment; filename=MyFile.pdf");
  Response.TransmitFile(Server.MapPath("~/Files/MyFile.pdf"));
  Response.End();

  第一步是设置文档内容的类型,上面的例子是下载一个.PDF格式的文件。下面是最常用的一些文档内容类型:
.htm, .html     Response.ContentType = "text/HTML";
.txt    Response.ContentType = "text/plain";
.doc, .rtf, .docx    Response.ContentType = "Application/msword";
.xls, .xlsx    Response.ContentType = "Application/x-msexcel";
.jpg, .jpeg    Response.ContentType = "image/jpeg";
.gif    Response.ContentType =  "image/GIF";
.pdf    Response.ContentType = "application/pdf";


方法二:解决.PDF文件较大时,可能导致所下载的PDF文件无法打开的方案
   
[C#] 纯文本查看 复制代码
 protected void Button1_Click(object sender, EventArgs e) 
     { 
         string path; 
         try 
         { 
             path = Request.PhysicalApplicationPath + "/" + Session["pdfpath"].ToString() + "/PDF/" + Session["mlmc"].ToString() + ".pdf"; 
         } 
         catch (Exception) 
         { 
             return; 
         } 
         System.IO.Stream iStream = null; 
         byte[] buffer = new Byte[10000]; 
         int length; 
         long dataToRead; 
         string filename = Session["mlmc"].ToString() + ".pdf"; 
  
         try 
         { 
             iStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read); 
             dataToRead = iStream.Length; 
             Response.Clear(); 
             Response.ClearHeaders(); 
             Response.ClearContent(); 
             Response.ContentType = "application/pdf"; //文件类型 
             Response.AddHeader("Content-Length", dataToRead.ToString());//添加文件长度,进而显示进度 
             Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)); 
             while (dataToRead > 0) 
             { 
                 if (Response.IsClientConnected) 
                 { 
                     length = iStream.Read(buffer, 0, 10000); 
                     Response.OutputStream.Write(buffer, 0, length); 
                     Response.Flush(); 
  
                     buffer = new Byte[10000]; 
                     dataToRead = dataToRead - length; 
                 } 
                 else 
                 { 
                     dataToRead = -1; 
                 } 
             } 
              
         } 
         catch (Exception ex) 
         { 
             Response.Write("文件下载时出现错误!"); 
         } 
         finally 
         { 
             if (iStream != null) 
             { 
                 iStream.Close(); 
             } 
             //结束响应,否则将导致网页内容被输出到文件,进而文件无法打开 
             Response.End(); 
         }         
     } 


  通过以上代码,可在浏览器中打开一个“打开/保存”对话框来下载并保存文件。



C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
chao2332601 | 2013-6-15 23:53:46 | 显示全部楼层
谢谢分享!!!
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则