zoukankan      html  css  js  c++  java
  • ASP.net 文件下載

    用时候当系统文件关联直接下载的话会调用程序打开,或者想验证后才能给用户下载,那么可以用这个方法实现

     private void FileDown(string strPath)
        {
            System.IO.FileInfo file = new System.IO.FileInfo(strPath);
            if (file.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.FullName, System.Text.Encoding.UTF8));
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.Filter.Close();
                Response.WriteFile(file.FullName);
                Response.End();
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "", "<script language='javascript'>alert('文件不存在!');</script>");
            }
        }

    顺便发一下打GridView转成Excel让用户下载的代码

    private void DownExcel()
    {
            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=print.xls");
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            GridView1.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
    }
  • 相关阅读:
    定制一个类似地址选择器的view
    3D版翻页公告效果
    一分钟搞定触手app主页酷炫滑动切换效果
    苹果版小黄车(ofo)app主页菜单效果
    基于SpringMVC+Ext.js的权限管理系统(无权限框架)
    使用 mybatis + flying-0.9.4 的电商后端
    iOS仿支付宝首页效果
    Android蓝牙
    JavaWeb Session详解
    原生JS实现的h5小游戏-植物大战僵尸
  • 原文地址:https://www.cnblogs.com/twttafku/p/1212609.html
Copyright © 2011-2022 走看看