0

I have a pdf file in a database that I loaded in the controller and directly want to return via return File

return File(document.Data, document.ContentType);

Data is the bytearray with ~76000 bytes ContentType is the application/pdf

When I want to view the result in webbrowser (either FF or Chrome) I get to see the pdf code instead of the pdf.

%PDF-1.4 %���� 1 0 obj <>stream x��� and so on

Would appreciate any help because it must be so simple, but I can't find it.

The return is placed in ActionResult Index and I need it to be loaded right at click on page (no new page with _blank)

It is the right data, because when I use f12 in chrome and click on network and the data I get to view the pdf as a whole

Edit1:

[HttpGet]
    public ActionResult Index(int Id)
    {
        InitMvcApplicationMenu();

...

        var document = WebApi.LoadDocument(DocumentGuid.Value);
        var byteArray = document.Data;
        if (byteArray == null)
        {
            return null;
        }
        Stream stream = new MemoryStream(byteArray);


        if (document == null)
        {
            return View("NoDocument");
        }

        Response.AppendHeader("content-disposition", "inline; filename=file.pdf");
        return File(stream, document.ContentType, "document.pdf");
    }

This way I get an error no file was found. When I use it the way before with

return File(document.Data, document.ContentType);

I get the bytearray as view instead of a pdf, but the file is found

Edit 2:

public ActionResult Index(int Id)
    {
        InitMvcApplicationMenu();
        var entity = WebApi.LoadItem(Id);
        var DocumentGuid = entity.ReportDocumentGUID;
        if (DocumentGuid == Guid.Empty)
        {
            return View("NoDocument");
        }
        var document = WebApi.LoadItem(DocumentGuid.Value); 

        if (document == null)
        {
            return View("NoStatusReportDocument");
        }

        var cd = new ContentDisposition
        {
            FileName = document.Name,
            Inline = true
        };

        Response.Headers.Add("Content-Disposition", cd.ToString());
        return File(document.Data, document.ContentType);
    }    

I have a Wrapper with multiple registertabs and want to show the pdf inside the tab when the document tab is selected.

This happens here:

    my.onHashChanged = function (e) {

    var feature = jHash.val('feature');

    my.loadTab(feature);
}

 my.loadTab = function (feature) {
    if (feature) {
        $("#tabstrip-content").html("Loading...");
        applicationUIModule.loadPartialView(feature, "Index", { Id: $("#Id").val()}
            , function (data) {


            }
            , null
            , $("#tabstrip-content")
        );
    }
}

    my.onTabSelect = function (e) {
    var feature = $(e.item).data("feature");
    applicationUIModule.updateHash("feature", feature);
}
Lukas
  • 35
  • 1
  • 11
  • Are you setting the Content Disposition? (See [this answer](https://stackoverflow.com/a/5830215/5443550) for an example) – Diado Apr 25 '18 at 13:29
  • Negative, I could set it, but still only cryptic code instead of a viewable pdf. Thanks still! – Lukas Apr 25 '18 at 14:40
  • please show the code to explain exactly how you're initialising this request, it's a little unclear from your description. You're not trying to do this via AJAX, by any chance? – ADyson Apr 25 '18 at 15:26
  • included the js and the whole ActionResult for you – Lukas Apr 25 '18 at 15:48
  • The Question is, what return value do I need in the Controller so I can show the PDF on loading the tab without needing an extra view – Lukas Apr 25 '18 at 15:55
  • The PDF is a separate file from your HTML, so you have to provide it in a separate tab or iframe. It will always entail a separate HTTP request. An iframe would allow you to embed it within the main view – ADyson Apr 25 '18 at 18:46
  • So a view will be needed with an Iframe and loading the file into it – Lukas Apr 26 '18 at 06:21

1 Answers1

0

This is what you have to do:

[HttpGet]
public IActionResult document(int id)
{
      var byteArray = db.instances.Where(c => c.id == id).FirstOrDefault().document;    
      if (byteArray == null)
      {
            return null;
      }
      Stream stream = new MemoryStream(byteArray);

      Response.AppendHeader("content-disposition", "inline; filename=file.pdf");
      return File(stream, "application/pdf", "document.pdf");
}