I have searched the web for an answer but without any luck. I'm wondering how and if I'm able to render a pdf-file using Razor into a iFrame located in my view. The pdf is a byte array and is loaded in my Model.
This is my code so far:
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");
}
My Model:
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);
}
And my view:
<div class="tab-pane active" id="dokument">
<iframe src="@Url.Action("ByteConverter", "RegulationController")"></iframe>
</div>
byte[]into your action - where does that value get populated?iframeis loading from a URL that doesn't include a value forpdfDataand so I wouldn't expect that to work as is. Is thatByteConvertermethod an action or a helper function that you're calling from an action? As an aside, you shouldn't need the call toStream.Writeas theMemoryStreamconstructor you're using already sets that data (I don't think you'll need the following line either in that case).