0

I have an html file in my asp.net webforms project that people can download when clicking a button.

I would like to generate some <select> lists and append to certain parts of the html based on some database values before the file gets sent to the user. Is this possible using c#? My current download functionality:

public void DownloadOfflineAuditSheetEditor(object s, EventArgs e)
{
    Response.AppendHeader("content-disposition", "attachment; filename=thefile.html");
    Response.WriteFile(Server.MapPath("~/thefile.html"), true);
    Response.End();
}
2
  • 2
    Have a look at the HtmlAgilityPack Commented Sep 14, 2012 at 9:28
  • @nonnb Thans for the tip, looks nice! Commented Sep 14, 2012 at 11:29

2 Answers 2

2

Yes - you'd have to manipulate "the file" before it gets sent to the user.

In your method DownloadOfflineAuditSheetEditor you could have call a new method that reads the current file, gets the contents from the DB and then writes to the file or a new file, for example:

public void GenerateRealTimeContent() 
{

   var path = Server.MapPath("~/thefile.html");
   var dbContent = Database.GetContent(); // returns the <select> Options
   string[] lines = System.IO.File.ReadAllLines(path);
   StringBuilder sb = new StringBuilder();

   foreach (var line in lines) 
   {
     if (line == "CONTENT WHERE YOU WANT TO EDIT") 
     {
        SB.AppendLine(dbContent);
     }

     SB.AppendLine(line);
   }

  // code to write to your file

}

Then in your original function do:

public void DownloadOfflineAuditSheetEditor(object s, EventArgs e)
{
   GenerateRealTimeContent();
   Response.AppendHeader("content-disposition", "attachment; filename=thefile.html");
   Response.WriteFile(Server.MapPath("~/thefile.html"), true);
   Response.End();
}

http://msdn.microsoft.com/en-us/library/ezwyzy7b.aspx - Reading from a file

http://msdn.microsoft.com/en-us/library/aa287548(v=vs.71).aspx - Write to a file

Sign up to request clarification or add additional context in comments.

1 Comment

Agreed, and I he uses xhtml instead of html for the file's content, he can use LinqToXml to easily add the real-time content prior to streaming it to the client.
0

You can read the file with a StreamReader, edit it or add anything and write the all thing in the Resposne.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.