当前位置:编程学习 > C#/ASP.NET >>

.net C# 实现点击下载PDF文件(通知浏览器打开下载对话框) 不是直接打开

[csharp]  string FullFileName = ""; 
           try 
           { 
               string FileName = "拓鹏云信接口文档 v2.0.pdf"; 
               FullFileName = Server.MapPath(FileName); 
               //FileName--要下载的文件名  
               FileInfo DownloadFile = new FileInfo(FullFileName); 
               if (DownloadFile.Exists) 
               { 
                   Response.Clear(); 
                   Response.ClearHeaders(); 
                   Response.Buffer = false; 
                   Response.ContentType = "application/octet-stream"; 
                   Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.ASCII)); 
                   Response.AppendHeader("Content-Length", DownloadFile.Length.ToString()); 
                   Response.WriteFile(DownloadFile.FullName); 
                   Response.Flush(); 
                   Response.End(); 
               } 
               else 
               { 
                   //文件不存在  
                   
               } 
           } 
           catch(Exception ex) 
           { 
               //打开时异常了  
               
           }   

 string FullFileName = "";
            try
            {
                string FileName = "拓鹏云信接口文档 v2.0.pdf";
                FullFileName = Server.MapPath(FileName);
                //FileName--要下载的文件名
                FileInfo DownloadFile = new FileInfo(FullFileName);
                if (DownloadFile.Exists)
                {
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.Buffer = false;
                    Response.ContentType = "application/octet-stream";
                    Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.ASCII));
                    Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                    Response.WriteFile(DownloadFile.FullName);
                    Response.Flush();
                    Response.End();
                }
                else
                {
                    //文件不存在
                  
                }
            }
            catch(Exception ex)
            {
                //打开时异常了
              
            } 

 

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,