Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Download Button in ASP.NET

Best Practices

Download Button in ASP.NET

by  christheprogrammer  Posted    (Edited  )
The following is how you enable a download button on an ASP.NET webform. Sorry but some lines are long and may autowrap.

Just make sure you have a button named "saveAsButton" on your webform and a textfile named "SomeFile.txt" in the same directory as your aspx page to which this codebehind file belongs.

When the user clicks the button, the standard "File Download" box will pop up and the user will be able to choose where to save it. I hope this helps!

Private Sub saveAsButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles saveAsButton.Click
Dim filename As String = "SomeFile.txt"
Dim filepath As String = MapPath(filename)
' In the following line, those are 4 quote marks each
Dim contentString As String = "attachment; filename=" & """" & filename & """"

Response.AddHeader("content-disposition", contentString)
Response.WriteFile(filepath)
Response.End()
End Sub

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top