>
FileResult
>FileStreamResult
ContentDisposition
>
> FileStreamResult
>
// Opens known types, downloads unknown types (incorrect filename/extension) return new FileStreamResult(new MemoryStream(document.Data), document.ContentType);
<>>提供一个简单的解决方案,但对于未知文件类型可能是不可靠的,通常默认为下载。 要获得更健壮的控制,请考虑使用File
或操纵ContentDisposition
标题。
public ActionResult Download() { var document = ...; var cd = new System.Net.Mime.ContentDisposition { FileName = document.FileName, Inline = false, // Forces download }; Response.AppendHeader("Content-Disposition", cd.ToString()); return File(document.Data, document.ContentType); }
>提供妥协:ContentDispositionHeaderValue
public IActionResult Download() { var document = ...; var cd = new ContentDispositionHeaderValue("attachment") { FileNameStar = document.FileName // Use FileNameStar for better encoding }; Response.Headers.Add(HeaderNames.ContentDisposition, cd.ToString()); return File(document.Data, document.ContentType); }
标题:
使用> 为了改善国际角色支持,建议班级:以上是如何在ASP.NET MVC中控制文件显示(查看或下载)?的详细内容。更多信息请关注PHP中文网其他相关文章!