GridView 动态绑定文件名称点击可下载

htmlCode :

 <asp:GridView ID="gvData" runat="server" AutoGenerateColumns="false" CssClass="gridview" EmptyDataText="暂无产品信息"
            Width="99%" RowStyle-HorizontalAlign="center" OnDataBound="gvData_DataBound" OnRowCommand="gvData_RowCommand"  >
            <RowStyle Height="23px" />
            <Columns>
                <asp:TemplateField HeaderText="序号">
                    <ItemTemplate>
                        <%#Container.DataItemIndex + 1 %>
                    </ItemTemplate>
                    <ItemStyle Width="30px" />
                </asp:TemplateField>
                <asp:BoundField DataField="supplier_name" HeaderText="供应商" />
                <asp:BoundField DataField="file_name" HeaderText="文件名" />
               <asp:BoundField DataField="file_type" HeaderText="文件类型" />
                 <asp:BoundField DataField="update_date" HeaderText="日期" />
                
             <asp:TemplateField HeaderText="操作">
                    <ItemTemplate>
                       <img  src="../images/gif-0068.gif"/> <asp:LinkButton ID="lnkbtnDown"  CssClass="lnkbtn" runat="server" CommandName="DOWN" OnClick="lnkbtndown_Click"  CommandArgument='<%#Eval("filename") %>'>下载</asp:LinkButton>
                    </ItemTemplate>
                
                    <ItemStyle Width="150px" />
                </asp:TemplateField>
            </Columns>
            <RowStyle HorizontalAlign="Center" />
            <EmptyDataRowStyle ForeColor="Red" HorizontalAlign="Center" />
        </asp:GridView>

后台代码:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Loading();
           
        }
    }

 private void Loading()
    {
      //file_type(文件类型字段1表示文件放在reportfiles文件夹,2表示文件放在yieldratereportfile文件夹)file_name(文件名字段)
        string strSql = "select A.ID,supplier_name,file_name,A.update_man,update_date,file_type,file_type+'#'+file_name as filename from supplier_file A,sqe_supplierinfo B where A.supplier_id = B.Supplier_ID ";
       
        DataTable dt = fun.GetDataTable(strSql);
        gvData.DataSource = dt;
        gvData.DataBind();
        dt.Dispose();
    }
   

 protected void lnkbtndown_Click(object sender, EventArgs e)
    {
        string strFullName = (sender as LinkButton).CommandArgument;
        string[] strName = strFullName.Split('#');
        if (strName[0].ToString() == "1")//此处分两个文件夹
        {
            FileDownload(System.Web.HttpContext.Current.Server.MapPath("../") + "reportfiles\\" + strName[1]);
        }
        else
        {
            FileDownload(System.Web.HttpContext.Current.Server.MapPath("../") + "yieldratereportfiles\\" + strName[1]);
        }
      
    }

  public void FileDownload(string strFullName)//下载文件绝对路径
    {
        if (File.Exists(strFullName))
        {
            FileInfo DownloadFile = new FileInfo(strFullName);
            Response.Clear();
            Response.ClearHeaders();
            Response.Buffer = false;
            Response.ContentType = "application/octet-stream";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
            Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
            Response.WriteFile(DownloadFile.FullName);//DownloadFile.FullName保存文件名
            Response.Flush();
            Response.End();
        }
        else
        {
            Response.Write("<script>alert('文件不存在');</script>");
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值