asp转发器下载文件c#

时间:2013-06-18 15:48:48

标签: c# asp.net repeater

我正在创建一个网站,在asp.net中并使用C#我的问题是我创建了一个asp转发器工作正常所以我添加了一个图像按钮来下载转发器包含的文件,但我不能让它下载文件,希望有人可以帮助我,我已经有了这个,我尝试了几种方法,但可以让它做我想要的。这是我的代码。

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();
    }
}

1 个答案:

答案 0 :(得分:0)

OnItemCommand = "Dwn"中的方法名称必须与代码隐藏中的方法名称匹配。将Save方法重命名为Dwn,或更改标记以匹配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();
}

将断点放在条件(如果...)上,看看断点是否被击中。