I'm writing code to perform a rather simple task but it appears to be more difficult than it should be. I have a fileContent string, containing a long string item, which I have generated within my code and wish to save and a path variable which contains the new path I wish to save the file to.
When I debug the code, the fileContent variable contains the string in full but, once the file is downloaded and saved, however, there is no text inside it.
I tried writing the fileContent string as an array of bytes using the same StreamReader object I created but made things worse as I had file process errors.
Why might this be and what can I do to solve this problem?
Code:
/*
* Process of exporting file to save it as a single document, ensuring
* that the correct extension is used.
*/
private void ExportToSave(string path, string fileContent)
{
HttpResponse hr = HttpContext.Current.Response;
hr.AddHeader("Content-Disposition", "attachment;filename=ProRefExport");
StreamWriter sw = new StreamWriter(path);
HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
sw.WriteLine(string.Format("Generated by user on", DateTime.Today));
sw.Close();
htmlWriter.Close();
File.WriteAllText(path, fileContent, System.Text.Encoding.ASCII);
hr.ContentType = "text/html";
hr.End();
base.Render(htmlWriter);
}
I tried writing the fileContent string as an array of bytes using the same StreamReader object I created but made things worse as I had file process errors.
Why might this be and what can I do to solve this problem?
Comment