Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display pdf in a iframe from a byte array c# MVC .NET Core

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>
like image 604
K.Oleksiak Avatar asked Dec 06 '18 09:12

K.Oleksiak


2 Answers

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.

like image 80
K.Oleksiak Avatar answered Oct 06 '22 00:10

K.Oleksiak


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.

like image 31
Nan Yu Avatar answered Oct 05 '22 23:10

Nan Yu