Revert PDF to browser using JSON and MVC?

I have a link as follows.

@Html.ActionLink("Create Report", "Screenreport", "Reports", null, new { @class = "subNavA AddBorderTop", id = "screenReport", title = "Create Report" }) 

After clicking the link, I have the following jQuery code that creates a JSON object and publishes the information.

 $().ready(function () { // Create Report fron the screen data $("#screenReport").live("click", function (event) { GenerateScreenReport(this, event); }); }) /* end document.ready() */ function GenerateScreenReport(clikedtag, event) { var table = $(".EvrakTable").html(); var screendata = tableParser(table); var Screentable = { Screenlist: screendata }; var myurl = $(clikedtag).attr("href"); var title = $(clikedtag).attr("title"); $.ajax({ url: myurl, type: 'POST', data: JSON.stringify(Screentable), dataType: 'json', contentType: 'application/json', success: function () { alert("Got it"); } }); }; 

To handle JSON, I have the following two classes. Implement two classes in the same namespace

 namespace MyProject.ViewModels { public class Screenrecord { public string Fname{ get; set; } public string LName { get; set; } public string Age { get; set; } public string DOB { get; set; } } public class Screentable { public List<Screenrecord> Screenlist { get; set; } } } 

ANd in my controller, I have the following code:

  [HttpPost] public FileStreamResult Screenreport(Screentable screendata) { MemoryStream outputStream = new MemoryStream(); MemoryStream workStream = new MemoryStream(); Document document = new Document(); PdfWriter.GetInstance(document, workStream); document.Open(); document.Add(new Paragraph("Hello World")); document.Add(new Paragraph(DateTime.Now.ToString())); document.Close(); byte[] byteInfo = workStream.ToArray(); outputStream.Write(byteInfo, 0, byteInfo.Length); outputStream.Position = 0; return new FileStreamResult(outputStream, "application/pdf"); } 

This code should transmit PDF. if I leave [HttpPost] as it is, it will NOT generate a PDF and it will go to the / Screenreport page, however, I can see that my JSON is correctly transferred to the controller. (screendata fills correctly - in the controller)

But if I comment on [HttpPost], it generates a PDF, but screendata (in the controller) is null.

Can someone explain what is happening and help me figure it out. Thank you in advance.

+3
source share
2 answers

I feel obligated to publish my answer since I have not heard from anyone. I ended up creating a form that contains hidden input, and then saved my json object in hidden input and then submit the form. This time I will get the input as a string, not json or xml.

 var $hidInput = $("#dataToReport"); $hidInput.val(JSON.stringify(Screentable)); $('#frmScreenreport').submit(); 

Thanks to everyone.

+1
source

You cannot use AJAX to upload files because javascript does not allow you to save downloaded content. To get around this, you need to follow 2 steps .

First: execute the request for the HTTP message, and in the controller action we will save the contents of the File in the memory stream. Second: if you make another call successfully by setting window.location to the load action method

In your controller, create these 2 actions:

 public ActionResult GenerateFile() { MemoryStream fileStream = new MemoryStream { Position = 0 }; //position = 0 is important var fName = string.Format("File-{0}.xlsx", DateTime.Now.ToString("s")); Session[fName] = fileStream; return Json(new { success = true, fName }, JsonRequestBehavior.AllowGet); } public ActionResult DownloadFile(string fName) { var ms = Session[fName] as MemoryStream; if (ms == null) return new EmptyResult(); Session[fName] = null; return File(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fName); } 

In your javascript:

 $('#Donwload-button').click(function () { data = JSON.stringify(YOURDATA); $.ajax({ contentType: 'application/json; charset=utf-8', dataType: 'json', type: 'POST', url: "/YOURCONTROLLER/GenerateFile", data: data, success: function (d) { if (d.success) { window.location = "/YOURCONTROLLER/DownloadFile" + "?fName=" + d.fName; } }, error: function () { alert("Error"); } }); }); 
+4
source

All Articles