0

In ASP.NET Application. I need to return string (which contains all necessary html tags) to the client browser as downloadable HTML file.

So basically convert string to HTMLDocument and send it to the client.

How it can be done in asp.net c# application?

1

1 Answer 1

2

Essentially there are four things you would want to do:

  1. Clear anything currently in the Response buffer. A call to Response.Clear(); will take care of this.
  2. Set the appropriate response headers. In the case of wanting to return a downloadable file, you'd set the content-disposition header as such: Response.AddHeader("content-disposition", "attachment; filename=somefile.html"); (Where "somefile.html" is the name to suggest that the browser use for saving the file. It can be hardcoded or a dynamic value, that's up to you.) This is the standard way to suggest to a browser that it should save the file. I say "suggest" because it's not required. Understand that HTTP has no concept of "files." Only requests and responses. What you're doing with this header is telling the browser that the data stream you're sending it should be saved as a file. It's highly likely that the browser will comply with this.
  3. Write the data. Response.Write(); is the usual way to do this.
  4. End the response (so the browser can be signaled that it's received everything). Response.End(); does this.

Take a look at the other options available on the HttpResponse class, though. There are even methods called WriteFile() which may make the code even simpler. I'm not familiar with them, so I can't really give an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.