zoukankan      html  css  js  c++  java
  • ASP.NET 文件下载 .

    <a href="download.ashx?url=<%=Server.UrlEncode("111.txt")%>">下载</a>

    download.ashx

    public void ProcessRequest(HttpContext context)
            {
                string url = HttpContext.Current.Server.UrlDecode(context.Request.QueryString["url"]);
                downloadfile(url);
            }

            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
            public void downloadfile(string s_fileName)
            {
                HttpContext.Current.Response.ContentType = "application/ms-download";
                string s_path = HttpContext.Current.Server.MapPath(".") + s_fileName;
                System.IO.FileInfo file = new System.IO.FileInfo(s_path);
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
                HttpContext.Current.Response.Charset = "utf-8";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
                HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
                HttpContext.Current.Response.WriteFile(file.FullName);
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.End();

            }

  • 相关阅读:
    @ControllerAdvice + @ExceptionHandler 使用
    将博客搬至CSDN
    Docker pull网络错误
    Centos7.5安装Docker
    Oracle18c创建不带C##的用户
    Centos7.5静默安装Oracle18c
    nodeJs和JavaScript的异同
    maven项目引入本地包,不使用中央仓库
    java中把指数形式的数字转为正常形式显示
    validateJarFile jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
  • 原文地址:https://www.cnblogs.com/zhengguangITelite/p/2506655.html
Copyright © 2011-2022 走看看