Consider the need to return a plain-text file from a controller method back to the caller. The idea is to have the file downloaded, rather than viewed as plaintext in the browser.
I have the following method, and it works as expected. The file is presented to the browser for download, and the file is populated with the string.
I'd like to look for a 'more correct' implementation of this method, as I am not 100% comfortable with the void
return type.
public void ViewHL7(int id)
{
string someLongTextForDownload = "ABC123";
Response.Clear();
Response.ContentType = "text/plain";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.hl7", id.ToString()));
Response.Write(someLongTextForDownload);
Response.End();
}
Use the File method on the controller class to return a FileResult
public ActionResult ViewHL7( int id )
{
...
return File( Encoding.UTF8.GetBytes( someLongTextForDownLoad ),
"text/plain",
string.Format( "{0}.hl7", id ) );
}
You'll want to return a FileContentResult
from your method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With