I have an ajax method .....
function downloadPDF() {
var elem = $('#actionPlanDetails');
elem.find("button").hide();
elem.find("iframe").hide();
elem.find("input[type='button']").hide();
html2canvas(elem[0], {
onrendered: function (canvas) {
var result = canvas.toDataURL();
var base64 = result.split(',')[1];
elem.find("button").show();
elem.find("iframe").show();
elem.find("input[type='button']").show();
$.ajax({
beforeSend: function () {
$.blockUI({ message: $('#waitMessage') });
},
url: "/ActionPlan/UploadPDF",
type: 'POST',
data: { pdfStr: base64 },
dataType: 'image/jpeg',
success: function (result) {
console.log('PDF');
},
complete: function () {
$.unblockUI();
}
});
}
});
}
...which uses html2canvas.js to get a screen image and send the bytes to an MVC action method....
[HttpPost]
public ActionResult UploadPDF(string pdfStr)
{
byte[] pdf = Convert.FromBase64String(pdfStr);
int fileLength = pdf.Length;
Document doc = new Document();
string pdfpath = Server.MapPath("../Content/pdfs");
try
{
PdfWriter.GetInstance(doc, new FileStream(pdfpath + "/Plan.pdf", FileMode.Create));
doc.Open();
Image planImg = Image.GetInstance(pdf);
doc.Add(planImg);
}
catch (Exception ex)
{
}
finally
{
//doc.Close();
}
return File("~/Content/pdfs/Plan.pdf", "application/pdf");
}
...which converts the passed string to a byte array, and uses iTextSharp to create a document, append it to the PDF document and write it to the Content subdirectory.
The issue comes in that, after writing the PDF file i also want it to pop up in a window on action return, as I've often seen the Controller.File() method do. But nothing happens. The PDF saves fine in the content folder but nothing pops up when the action returns.
looking at was is in the response in fiddler I just see a bunch of bytes, instead of a PDF, so I think maybe i'm leaving out some configuration/metadata, but I can't find much on this situation in the docs.