2

I have an Asp.Net MVC project and in my View this stretch:

$.ajax({
        beforeSend: function () {
            LoadStart();
        },
        complete: function () {
            LoadStop();
        },
        //async: false,
        contentType: 'application/json, charset=utf-8',
        dataType: 'json',
        type: 'POST',
        url: '@Url.Action("MyAction", "MyController")',
        data: JSON.stringify(
            {
                Param1: 1,
                Param2: 2
            }),
        success: function (data) {
            $("#pdf-content").show();
            // Here, fill DIV (pdf-content) with PDF.
        },
        fail: function () {
            alert("Fail");
        }
});


In MyAction, I have:

[HttpPost]
public ActionResult MyAction(int Param1, int Param2)
{
    // Code ...

    MemoryStream stream = new MemoryStream();
    outPdf.Save(stream);
    byte[] fileContents = stream.ToArray();

    return File(fileContents, "application/pdf", "myFile.pdf");
}


How can I display the PDF in DIV, or even download directly without displaying?

Jedaias Rodrigues
  • 316
  • 1
  • 6
  • 20
  • 1
    You cannot download a file using ajax. You need to navigate to a method that returns the file (e.g. you could use `location.href = ....` in your success callback) –  Mar 22 '16 at 12:06
  • @StephenMuecke Thank you for your comment, but do not give me bad news please. I need to use POST type. – Jedaias Rodrigues Mar 22 '16 at 12:11
  • @teovankot Thank you for you comment, but the question referred by you use GET type, but my question type use POST. – Jedaias Rodrigues Mar 22 '16 at 12:12
  • @JedaiasRodrigues you didn't read it all. It's first POST then GET – teo van kot Mar 22 '16 at 12:12
  • 2
    You can post the data to a POST method and save whatever you want, but you then need a GET to a method to download the file. –  Mar 22 '16 at 12:16
  • You're right! But still it is a little different because I have used MemoryStream instead of a physical file. Unfortunately I cannot save the file to disk. – Jedaias Rodrigues Mar 22 '16 at 12:18
  • 1
    @JedaiasRodrigues you don't need POSt in your case, just use GET – teo van kot Mar 22 '16 at 12:27
  • Unfortunately I have to use POST type. I am generating a report that can send many parameters, and besides a very large URL does not work, it is visually ugly. – Jedaias Rodrigues Mar 22 '16 at 12:37
  • Maybe the answer of Jonathan Amend (not the accepted answer!) to this question will help you http://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post – derpirscher Mar 22 '16 at 13:02
  • @StephenMuecke Your solution is the one that works, post an answer so that I can accept. – Jedaias Rodrigues Mar 22 '16 at 22:04

1 Answers1

1

In this case the best solution is: In your POST method save file at the server with unique name (GUID for example) and return this guid to client.

Then make get request, find your file by guid and return file to client and delete it from the server.

And it's very good to create service, that would delete all old files.

jarres
  • 71
  • 4