ASP.NET下载文件(弹出打开保存文件对话框)

看了好多种方法

好像都是通过Response转换来的


public void Open()
    {
 
        
string FullFileName = "";
        
        
//ASP.NET下载文件(弹出打开保存文件对话框) 
        
//fileURL为带路径的文件全名 
        /*
        string fileURL = url;
        System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileURL); 
        Response.Clear(); 
        Response.AddHeader("content-disposition","attachment;filename="+Server.UrlEncode(fileInfo.Name.ToString())); 
        Response.AddHeader("content-length",fileInfo.Length.ToString()); 
        Response.ContentType = "application/octet-stream"; 
        Response.ContentEncoding = System.Text.Encoding.Default; 
        Response.WriteFile(fileURL); 
         
*/

        
//无错版: 
        try
        {
            
string FileName = ".//路径//书名.pdf";
            FileName 
= ".//路径//文件名.扩展名";
            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
        {
           
//打开时异常了
        } 

    }
原文地址:https://www.cnblogs.com/futao/p/1635236.html