In my mind these two bits of code should work the same:
window.location = "../PlanView/ExportAsPDF";
$.ajax({
url: '../PlanView/ExportAsPDF',
data: { },
success: function (stream) { window.location = stream; }
});
The former bit of code triggers a .PDF file download for the user. The second one does not — I have a weird request pending in my browser’s network traffic.
Could someone highlight the key differences here that I should be aware of?
More in-depth explanation:
I need to pass more data to my server than is allowed in a URL. As such, I need to POST to the server instead of GET. I cannot use the former code because I cannot shove that much information into a URL — the server will respond with 414.
I would like to replicate the functionality of the former bit of code with the latter.
public ActionResult ExportAsPDF(string dataURL)
{
Document document = new Document(PageSize.A4.Rotate(), 15, 15, 30, 65);
byte[] buffer = new byte[0];
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter.GetInstance(document, memoryStream);
document.Open();
document.Add(new Paragraph("First PDF file"));
document.Close();
buffer = memoryStream.ToArray();
}
return File(buffer, "application/pdf", "PlanView.pdf");
}
The first one is different from the second because the first one tells the browser to display the file and the second one does not. Simply because “stream” won’t be “../PlanView/ExportAsPDF”.
Also there’s no need for this ajax request if you just want to direct the browser to the file. Also the latter if it at all works, would give you what – binary contents of the PDF file? Frankly I wouldn’t know how to handle that 🙂
You should also have “dataType” not “datatype” in your ajax attributes if you expect to get JSON back (which of course you won’t get).
UPDATE:
Even this should work: