0

My requirement is whenever a Image is clicked a PDF should be opened on the browser. I am using Jquery ajax [POST call] to make a call to the ASP.NET MVC side and a file is returned on the response. A POST is required from the jquery side since I need to pass substantial data from client to server.

HTML Part:

 <span data-id='printSettings' title='Generate PDF' class="preferenceSettings"></span>

JS Part: This is fired when the Generate PDF icon is clicked.

var textToSend = $('.microChartTable', self.element)[0];
var dataToSend = { htmlContent: textToSend.outerHTML };
$.ajax({
        url: "/EADashboard/ConvertToPDF",
        data: JSON.stringify(dataToSend),
        type: 'POST',                                        
        contentType: 'application/json',
        success: function (data) {

         } // -- success ends here

      });

ASP.NET Side : In my controller, I have the following code:

 [HttpPost]
    public FileResult ConvertToPDF(HtmContent content)
    {

        string fileName = Server.MapPath("~/SeedData/data.pdf");
        string contentType = "application/pdf";

        return new FilePathResult(fileName, contentType);
     }  

Now the PDF generation code is correct just that the PDF file is not been opened on the browser side. I have seen the post Return PDF to browser using JSON and MVC? but since there was no solution provided, I am posting this again. Can anyone let me know how this can be achieved ?

Thanks

1 Answer 1

1

Two things.

  1. Why are you doing post via ajax and not a regular post? With a regular post your code would probably work.

  2. If you indeed need to do it with ajax, you are receiving result in the data object on the success of the ajax call, and I do not see that you do anything with it, which is why you do not see anything happening.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.