Asp中继器下载文件c#

本文关键字:文件 下载 中继器 Asp | 更新日期: 2023-09-27 18:18:59

我正在创建一个网站,在asp.net和使用c#我的问题是我创建了一个asp中继器这是工作良好,所以我添加了一个imagebutton下载的文件,该中继器包含,但我不能让它下载该文件,希望有人能帮助我我已经有这个,我已经尝试了几种方法,但可以让它做我想要的。这是我的代码。

ASP

<asp:Repeater ID="RepDetailsPost" runat="server" OnItemCommand="Save" >
<HeaderTemplate>
<table style="width:500px" align ="center" cellpadding="0" class="rounded_corners" >
<tr style="background-color:Red; color:White">
<td valign="top" bgcolor="CC0000" >
<b>Post</b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:#333">
<td>
<table style="background-color:#222; width:500px" class="rounded_corners" >
<table>
<tr>
<td style="font-size:large">
Utilizador:
<asp:Label ID="lblSubject0" runat="server" Text='<%#Eval("nome_utilizador") %>' 
        Font-Size="small" Font-Bold="true"/>
</td>
</tr>
</table>
</td>
</tr>
<tr style="background-color:#333;Color:White">
<td align="right" >
Criado em:<asp:Label ID="lblDate0"   runat="server" Font-Bold="true" 
        Text='<%#Eval("data") %>'/>
</td>
</tr>
<tr>
<td>
<table style="background-color:#222; width:500px">
<tr> 
<td>Anexo:
    <asp:Image ImageUrl=<%# string.Format("~/uploads/{0}",Eval("Nome"))%> runat="server"  width=500px/>
</td>
</tr>
</table>
<table style="background-color:#222; width:500px">
<tr> 

<td>Guardar:
    <asp:ImageButton ID="save" ImageUrl=<%# string.Format("~/imagens/icones/save.png")%> runat="server"  width=30px CommandName="save"
   CommandArgument='<%# Eval("Nome") %>' />
        '''''''''''''''                    this is what I tried   on top     /////////////////
   </tr>
</table>
<table style="background-color:#222; width:500px" align=center>
<tr>
<td>Comentário: <br /><asp:Label ID="lblComment0" Font-Size="small" runat="server" Text='<%#Eval("descricao") %>' /></td>
</tr>
</table>
</td>
</tr>
<tr>
<td  colspan="2"valign="top" bgcolor="CC0000">&nbsp;
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>  
c#

   protected void Save(Object Sender, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "Save")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        string fName = "teste" ;
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fName);
        Response.TransmitFile(Server.MapPath("~/uploadsadmin/" + fName));
        Response.End();
    }
}

Asp中继器下载文件c#

OnItemCommand = "Dwn"中的方法名必须与代码隐藏中的方法名匹配。将Save方法重命名为down,或者更改标记以匹配Save方法名称。

这样重构代码:

if(e.CommandName == "Save")
{
    int index = Convert.ToInt32(e.CommandArgument);
    string fName = row.Cells[2].Text;
    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition", "attachment;filename=" + fName);
    Response.TransmitFile(Server.MapPath("~/uploadsadmin/" + fName));
    Response.End();
}

将断点置于条件语句(if…)上,并查看是否命中断点