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>

  否则会报错

 

posted on 2010-11-10 19:09  aparche  阅读(996)  评论(0编辑  收藏  举报