nihulus
nihulus

Reputation: 1495

.net MVC 4 return a pdf to browser

View

@Html.ActionLink("Förköpsinformation", "GetForkopsInfo", "RaknaTeckna", null, new { target = "_blank" })

The GetForkopsInfo() does the actionlink action, tm.PreSalesDocument is the pdf object in bytes

  [HttpPost]
   public ActionResult GetForkopsInfo()
    {

            tm = (TecknaMotor)Session["WinsureTecknaMotor"];
            tm.GetPreSalesDocument();


                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.ContentType = "Application/pdf";
                Response.AddHeader("content-length", tm.PreSalesDocument.Length.ToString());
                Response.AddHeader("Content-Disposition", "inline; filename = förköpsinfo.pdf");
                Response.BinaryWrite(tm.PreSalesDocument);
                return File(tm.PreSalesDocument, "Application/pdf", "förköpsinfo.pdf");

    }`

I want the controller to return the pdf to web browser and open a new tab. But it's not working at all, im not even sure it is correct to use actionlink? Don't know how to do it exactly...

Upvotes: 1

Views: 1179

Answers (1)

Botonomous
Botonomous

Reputation: 1764

Your attemtping to access an HttpPost ActionResult with an Actionlink which wont work out of the box. I reccomend you change the request to HTTPGet and have a look at HttpResponse.TransmitFile

Response.ContentType = "Application/pdf";
Response.TransmitFile(pathtofile);

And return that Response instead of the File Obj.

Alternatively, you can change to an HTTPGet and im fairly sure change your browser to open the PDF In a browser window.

Upvotes: 1

Related Questions