如何在ASP.net中编写下载按钮的代码

时间:2013-06-10 07:49:20

标签: c# asp.net

在我的数据库中,我已将pdf文件的地址保存为字符串类型,现在如何通过单击下载按钮来下载它?我需要帮助来编写下载按钮的代码......

    <td bgcolor="#CCE6FF" class="style7"> Chapter 1 <br /> 
                <asp:Button ID="Button6" runat="server" onclick="Button6_Click" Text="pdf"  />
              </td>

在我的数据库中有4列ChapterID(pk),Name,Document和SubjectId(fk)。在文档列中,我已将pdf文件地址保存为字符串。现在我想,当我点击下载按钮它应该能够下载文件...我该怎么做?????

2 个答案:

答案 0 :(得分:0)

尝试以上

byte[] data = File.ReadAllBytes(WDriveFilePath)

FileStream file = File.Create(HttpContext.Current.Server.MapPath(MyLocalFile)); 

file.Write(data, 0, data.Length); 
file.Close(); 

答案 1 :(得分:0)

        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=YourFilePath");
        Response.ContentType = "application/pdf";
        var sw = new StringWriter();
        var htw = new HtmlTextWriter(sw);
        //// Create a form to contain the grid
        var frm = new HtmlForm();
        frm.Attributes["runat"] = "server";
        frm.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();

希望这会有所帮助.. :)