从数据库下载文件 [英] Download file from the database

查看:62
本文介绍了从数据库下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我试图从数据库下载文件,但实际上没有发生任何事情。没有错误消息或任何东西。我甚至调试了代码。

代码如下:

Hi All
Iam trying to download file from the database but nothing actually happens. No error message or anything. I have even debug the code.
The code is as follows:

public void dwx(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "dw")
            {
                LinkButton lb = sender as LinkButton;
                DataListItem dlrow = lb.NamingContainer as DataListItem;
                string id = ((Label)dlrow.FindControl("Label9")).Text;
                connection();
                SqlCommand com = new SqlCommand("select FileName,FileType,Filedata from Newsu where Newsid=@id", con);
                com.Parameters.AddWithValue("@id", id);
                SqlDataReader dr = com.ExecuteReader();

                if (dr.Read())
                {
                    Response.Clear();
                    Response.Buffer = true;

                    Response.ContentType = dr["FileType"].ToString();
                    Response.AddHeader("content-disposition", "attachment;filename=" + dr["FileName"].ToString());
                    Response.Charset = "";
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.BinaryWrite((byte[])dr["Filedata"]);
                    Response.End();
                }
            }
        }









and

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
               <ContentTemplate>
                <asp:Panel ID="Panel1" runat="server" BackColor="White" Width="320px"

                       Height="792px" ScrollBars="Both" Visible="true" >
                    <asp:DataList ID="DataList1" runat="server" BackColor="White"

                        ToolTip="Download file" DataKeyField="Newsid" Font-Names="Segoe UI"

                        Width="300px" onitemdatabound="DataList1_ItemDataBound" GridLines="Both" >



                        <AlternatingItemStyle BackColor="#CCCCCC" />



                        <ItemTemplate>
                        <div class="headitem">
                        <div class="heads">
                         <asp:Label ID="Label6" runat="server" Text='<%# Eval("Emailattach") %>'  Visible="false"  ></asp:Label>
                          <asp:Label ID="Label2" runat="server" Text='<%# Eval("Subject") %>'

                                Font-Names="Segoe UI" Font-Size="Medium" ForeColor="White"></asp:Label></div>
                            &nbsp;&nbsp;


                            <asp:LinkButton ID="ImageButton1" runat="server" Height="21px"

                                 BorderStyle="Solid" BorderColor="#2A2A2A"

                                Width="22px" OnCommand="dwx" CommandName="dw" >
                            <img src="images/a.png" />
                            </asp:LinkButton>


                               </div>
                                <br />
                                <div class="line">
                           &nbsp;  <asp:Label ID="Label3" runat="server" Text='<%# Eval("Line") %>'

                                Font-Names="Segoe UI" ></asp:Label> &nbsp;&nbsp;
                                &nbsp;&nbsp; <asp:Label ID="Label7" runat="server" Text='<%# Eval("Departmentdesc") %>'

                                Font-Names="Segoe UI" Font-Size="Smaller"></asp:Label>
                                <br />
                                <br />
                          <asp:Label ID="Label4" runat="server" Text='<%# Eval("Email")%> ' ></asp:Label> &nbsp;
                            <asp:Label ID="Label8" runat="server" Text='<%#Eval("Phone") %> ' ></asp:Label>

                            <div class ="times">
                            <asp:Label ID="Label5" runat="server" Text='<%# Eval("Newstime") %>'></asp:Label>
                                <asp:Label ID="Label9" runat="server" Text='<%# Eval("Newsid") %>' Visible="false"></asp:Label>
                            </div>
                            </div>
                        </ItemTemplate>

                    </asp:DataList>

      </asp:Panel>

                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="Ticker1" EventName="Tick" />


                    </Triggers>
       </asp:UpdatePanel>







我已经放置了一个链接按钮,点击它后我想下载文件...

请帮助解决问题...谢谢




I have placed a link button and upon clicking it i want to download the file...
Please help where the issue could be...thanks

推荐答案

删除更新面板并检查或放置控件(下载按钮或更新面板外的链接。
Remove the update panel and check this or place the control(download button or link) outside the update panel.


这篇关于从数据库下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆