zoukankan      html  css  js  c++  java
  • gridview按钮列的下载功能

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
           
    //这里是模板列里面的按钮,需要设置CommandArgument ="<%# GridView1.Rows.Count %>",否则出错
            if (e.CommandName == "Download")
            {
                FileDownload(
    @"F:\music\6-渴望.mp3");
            }
        }

       
    private void FileDownload(string FullFileName)
        {
            FileInfo DownloadFile
    = new FileInfo(FullFileName);
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.Buffer
    = false;
            Response.ContentType
    = "application/octet-stream";
            Response.AppendHeader(
    "Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
            Response.AppendHeader(
    "Content-Length", DownloadFile.Length.ToString());
            Response.AddHeader(
    "Content-Transfer-Encoding", "binary");
            Response.ContentEncoding
    = System.Text.Encoding.GetEncoding("UTF-8");
            Response.WriteFile(DownloadFile.FullName);
            Response.Flush();
            Response.End();
        }

    注意:

      如果是在GridView在UpdatePanel下,就需要加入如下代码:

      <Triggers>
         
    <asp:PostBackTrigger ControlID="GridView1" />
      </Triggers>

      否则会报错

     

  • 相关阅读:
    poj3277 City Horizon
    60.左值右值以及类型判断
    59.C++与正则表达式
    57.C++处理转义字符
    56.lambda表达式与绑定以及伪函数和绑定
    55.函数模板指针匹配(模板自动匹配*多的)
    54.函数模板默认参数
    53.伪函数与函数绑定器
    52.模板的重载
    51.模板与引用
  • 原文地址:https://www.cnblogs.com/tianguook/p/1874130.html
Copyright © 2011-2022 走看看