zoukankan      html  css  js  c++  java
  • 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
            {
               
    //打开时异常了
            } 

        }
  • 相关阅读:
    Python中利用函数装饰器实现备忘功能
    selenium之 定位以及切换frame(iframe)
    selenium之操作ChromeDriver
    设置跑的次数
    l类型转换错误ClassCastException
    代码提交失败
    【cl】Json学习
    mysql下载安装
    mysql登录基本语句
    linux 递归删除目录文件
  • 原文地址:https://www.cnblogs.com/futao/p/1635236.html
Copyright © 2011-2022 走看看