控制器:
[HttpPost]
public ActionResult HtmlToPdf ( String html )
{
try
{
var filename = DateTime.Now.Ticks.ToString() + ".pdf";
using (MemoryStream ms = new MemoryStream())
{
var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4);
pdf.Save(Server.MapPath("~/Dumps/") + filename);
}
return Json(new { filename = filename });
}
catch ( Exception e )
{
return Json(new { msg = e.Message });
}
}
AJAX:
$('#download-as-pdf').click(function () {
//var resultsHtml = $(this).closest('html').html();
var resultsHtml = "<html><head></head><body><p>Hey there</p></body></html>";
$.ajax({
url: '/Answers/HtmlToPdf',
method: 'POST',
data: { html: resultsHtml },
success: function (retobj) {
console.log(retobj);
},
error: function (retobj) {
console.log("The error callback was called");//TEST
}
});
})
我知道控制器正在被调用,因为如果我设置了resultsHtml = undefined
,那么我会从控制器中找回错误
{msg: "Cannot save a PDF document with no pages."}
那么这里发生了什么?有什么想法吗?