p.c*_*ell 59 asp.net-mvc controller download
考虑需要将纯文本文件从控制器方法返回给调用者.这个想法是下载文件,而不是在浏览器中查看为纯文本.
我有以下方法,它按预期工作.该文件将显示给浏览器以供下载,文件将填充该字符串.
我想寻找这种方法的"更正确"的实现,因为我对void
返回类型不是很满意.
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();
}
Run Code Online (Sandbox Code Playgroud)
tva*_*son 132
使用控制器类上的File方法返回FileResult
public ActionResult ViewHL7( int id )
{
...
return File( Encoding.UTF8.GetBytes( someLongTextForDownLoad ),
"text/plain",
string.Format( "{0}.hl7", id ) );
}
Run Code Online (Sandbox Code Playgroud)