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>
Allright, so after a few days i managed to make it work by changing the Pdf value in my model to Convert.ToBase64String(result.Pdf) like this:
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 = Convert.ToBase64String(result.Pdf),
Published = result.Published,
Region = result.Region,
StructuredInfo = result.StructuredInfo,
Title = result.Title,
ValidThru = result.ValidThru
};
return View(viewModel);
}
And in my view I skiped the @Url.Action("ByteConverter", "RegulationController") completely and replaced it with:
<iframe src="data:application/pdf;base64,@Model.Pdf" type="application/pdf"></iframe>
Works like i charm in all webbrowsers exccept IE, and that's because Internet Explorer does not support the use of DATA URIs as the source of iframes.
You can modify your code like :
public ActionResult ByteConverter()
{
ApiClient api = new ApiClient("http://localhost:43674/ApiCore");
var result = await api.GetAsync();
var pdfData = result.Pdf;
MemoryStream Stream = new MemoryStream(pdfData);
Stream.Write(pdfData, 0 , pdfData.Length);
Stream.Position = 0;
return new FileStreamResult(Stream,"application/pdf");
}
In view :
<div class="tab-pane active" id="dokument">
<iframe src="@Url.Action("ByteConverter", "RegulationController")"></iframe>
</div>
Then delete ByteConverter(viewModel.Pdf);
in your index view .And also confirm you have set correct controller name , use Home
instead of HomeController
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With