Reputation: 77
I have a default page with a button that prompts user to download a "signature"
Which is basically an .html file with specific format (based on user info)
So currently I have an .aspx page but I'm not sure how to make the user download the "rendered HTML page from that aspx"
On the default page i have the following
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
Is it possible to render the aspx page in the background then somehow prompt the user to download it ( the resulted html) ?
Upvotes: 0
Views: 420
Reputation: 144
You could override the Render() method of the aspx file so it writes an html file:
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
All this would be in the aspx file to be converted to html (signature.aspx). I would say have your button click do a redirect to a new window that calls the aspx, and thus this method.
Upvotes: 2
Reputation: 32694
You're making it more difficult than it is. Simply download the file content as you would from any other website, store it in a string, write it to the response.
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
Of course, a better solution would be to put your code for generating the signature in a Class Library (.dll) and then call that as needed.
Upvotes: 3