Reputation: 35194
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();
}
Upvotes: 0
Views: 790
Reputation: 70728
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
Upvotes: 2
Reputation: 3119
You can read the file with a StreamReader, edit it or add anything and write the all thing in the Resposne.
Upvotes: 0