考虑需要将一个纯文本文件从控制器方法返回给调用者。这个想法是下载文件,而不是在浏览器中以明文形式查看。
我有以下方法,它的工作与预期的一样。该文件将显示给浏览器以供下载,并使用字符串填充该文件。
我想寻找这个方法的一个“更正确的”实现,因为我对void
返回类型并不是100%满意。
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();
}
发布于 2009-10-14 23:23:16
使用控制器类上的File方法返回一个FileResult
public ActionResult ViewHL7( int id )
{
...
return File( Encoding.UTF8.GetBytes( someLongTextForDownLoad ),
"text/plain",
string.Format( "{0}.hl7", id ) );
}
发布于 2009-10-14 23:21:40
你需要从你的方法返回一个FileContentResult
。
https://stackoverflow.com/questions/1569532
复制相似问题