【发布时间】:2019-05-07 23:49:21
【问题描述】:
我在网上搜索了答案,但没有任何运气。我想知道如何以及是否能够使用 Razor 将 pdf 文件渲染到位于我视图中的 iFrame 中。 pdf 是一个字节数组,并加载到我的模型中。
这是我目前的代码:
public ActionResult ByteConverter(byte[] pdfData)
{
MemoryStream Stream = new MemoryStream(pdfData);
Stream.Write(pdfData, 0 , pdfData.Length);
Stream.Position = 0;
return new FileStreamResult(Stream,"application/pdf");
}
我的模特:
public async Task<ActionResult> Index()
{
ApiClient api = new ApiClient("http://localhost:43674/ApiCore");
var result = await api.GetAsync();
RegulationViewModel viewModel = new RegulationViewModel
{
ConnectedToRoadMap = result.ConnectedToRoadMap,
Decided = result.Decided,
Enforced = result.Enforced,
Id = result.Id,
Paragraph = result.Paragraph,
Pdf = result.Pdf,
Published = result.Published,
Region = result.Region,
StructuredInfo = result.StructuredInfo,
Title = result.Title,
ValidThru = result.ValidThru
};
ByteConverter(viewModel.Pdf);
return View(viewModel);
}
我的看法:
<div class="tab-pane active" id="dokument">
<iframe src="@Url.Action("ByteConverter", "RegulationController")"></iframe>
</div>
【问题讨论】:
-
iframe 为空。正文或标题中没有内容。
-
我看到您正在将
byte[]传递到您的操作中 - 该值在哪里填充? -
字节 [] 填充在我们正在构建的单独 API 中,该 API 从数据库中获取值。 API 获取数据并将其发送到我们的 WebbApp。所以基本上我得到了我需要的所有数据,我只需要在视图中呈现数据。这个特定的参数(pdfData)从模型中获取数据。
-
在您的问题中,您的
iframe似乎是从一个不包含pdfData值的 URL 加载的,所以我不希望它按原样工作。ByteConverter方法是您调用 from 动作的动作还是辅助函数?顺便说一句,您不需要调用Stream.Write,因为您正在使用的MemoryStream构造函数已经设置了该数据(我认为在这种情况下您也不需要以下行)。跨度> -
从模型中调用 ByteConverter 方法,我将编辑我的帖子并包含模型,以便您查看它的外观。
标签: c# pdf razor iframe asp.net-core