Попробуйте использовать FileStreamResult
в качестве типа возврата вместо FileResult
:
public ActionResult Download(string fileName)
{
var stream = new FileStream(Server.MapPath("~/files/examples/" + fileName), FileMode.Open, FileAccess.Read);
var result = new FileStreamResult(stream, "application/pdf");
return result;
}
Затем поместите тег <embed>
внутри модального <div>
элемента в качестве заполнителя PDF:
<div id="PedigreesSireRacingModal" class="modal-body racing">
<embed src="@Url.Action("Download", "ControllerName", new { fileName = "FileName.pdf" })" width="100%" height="100%" type="application/pdf">
</embed>
</div>
Обновление 1:
Поскольку вы можете вернуть формат HTML или PDF, установите флажок для расширения файла и используйте правильный тип содержимого в контроллере и теге embed
/ object
:
Контроллер
public ActionResult Download(string fileName)
{
string path = Server.MapPath("~/files/examples/" + fileName);
var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
string contentType = MimeMapping.GetMimeMapping(path);
var result = new FileStreamResult(stream, contentType);
return result;
}
View
<!-- example if using viewmodel property -->
<object data="@Url.Action("Download", "ControllerName", new { fileName = Model.FileName })" width="100%" height="100%">
</object>
<embed src="@Url.Action("Download", "ControllerName", new { fileName = Model.FileName })" width="100%" height="100%" type="@Model.ContentType">
</embed>
В качестве примечания попробуйте использовать Content-Disposition
параметр заголовка ответа в методе действия контроллера, чтобы убедиться, что тип файла установлен правильно:
var contentDisposition = new System.Net.Mime.ContentDisposition
{
FileName = fileName,
Inline = true
};
Response.Headers.Add("Content-Disposition", contentDisposition.ToString());