提示用户下载.aspx生成的.html页面

时间:2014-04-17 18:25:52

标签: html asp.net vb.net download

我有一个默认页面,其中有一个按钮,提示用户下载"签名"

这基本上是一个具有特定格式的.html文件(基于用户信息)

所以目前我有一个.aspx页面,但我不确定如何让用户从该aspx下载#34;呈现的HTML页面"

在默认页面上,我有以下

   Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.ContentType = "text/html"
        Response.AppendHeader("Content-Disposition", "attachment; filename=My_Signature.html")
        Response.TransmitFile(Server.MapPath("~/Signature.aspx"))
        Response.End()

    End Sub

是否可以在后台呈现aspx页面,然后以某种方式提示用户下载它(生成的html)?

2 个答案:

答案 0 :(得分:3)

你让它变得更难。只需像下载任何其他网站一样下载文件内容,将其存储在字符串中,然后将其写入响应。

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.ContentType = "text/html"
        Response.AppendHeader("Content-Disposition", "attachment; filename=My_Signature.html")
        Dim contents As String = New System.Net.WebClient().DownloadString(Request.Url.GetLeftPart(UriPartial.Authority) + ResolveUrl("~/Signature.aspx"))    
        Response.Write(contents)
        Response.End()    
    End Sub

当然,更好的解决方案是将您的代码用于在类库(.dll)中生成签名,然后根据需要调用它。

答案 1 :(得分:2)

您可以覆盖aspx文件的Render()方法,以便它写入一个html文件:

Protected Overrides Sub Render(writer As HtmlTextWriter)
    Dim sb As New StringBuilder()
    Dim sw As New StringWriter(sb)
    Dim hwriter As New HtmlTextWriter(sw)
    MyBase.Render(hwriter)
    Using outfile As New StreamWriter(Server.MapPath(".") + "\signature.html")
        outfile.Write(sb.ToString())
    End Using
    Response.ContentType = "text/html"
    Response.AppendHeader("Content-Disposition", "attachment;   filename=signature.html")
    Response.TransmitFile(Server.MapPath("~/signature.html"))
    Response.End()
End Sub

所有这些都将在aspx文件中转换为html(signature.aspx)。我会说你的按钮点击做一个重定向到一个调用aspx的新窗口,因此这个方法。