zoukankan      html  css  js  c++  java
  • asp.net下载文件方法

     /// <summary>
    /// 下载
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public static bool DownLoadFile(string url)
    {
    var flag = false;
    try
    {
    var filePath = HttpContext.Current.Server.MapPath(url); //获取文件的路径
    var file = new FileInfo(filePath); //得到文件
    if (file.Exists) //判断文件是否存在
    {
    HttpContext.Current.Response.Clear(); //清空Response对象
    /*设置浏览器请求头信息*/
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(file.Name)); //指定文件
    HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString()); //指定文件大小
    HttpContext.Current.Response.ContentType = "application/application/octet-stream"; //指定输出方式
    HttpContext.Current.Response.WriteFile(file.FullName); //写出文件
    HttpContext.Current.Response.End(); //结束Response对象
    HttpContext.Current.Response.Flush(); //输出缓冲区(刷新Response对象)
    HttpContext.Current.Response.Clear(); //清空Response对象

    flag = true;
    }
    else
    {
    flag = false;
    }
    }
    catch (Exception)
    {
    flag = false;
    }
    return flag;
    }

  • 相关阅读:
    Zabbix 3.2.1 安装 Graphtree3.0.4 或 Graphtree3.2.x
    jquery-1
    AngularJS (1)
    css-2 (Text Font)
    css
    Java经验
    js经验
    mysql经验
    MySQL 函数
    jquery 效果
  • 原文地址:https://www.cnblogs.com/xy0710/p/4791812.html
Copyright © 2011-2022 走看看