【问题标题】:Why am I getting a 500 Internal Error, no caught exceptions?为什么我收到 500 内部错误,没有捕获到异常?
【发布时间】:2016-06-08 23:18:38
【问题描述】:

控制器:

    [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."}

那么这里发生了什么?有什么想法吗?

【问题讨论】:

  • 你有没有弄明白这件事?我遇到了同样的问题。

标签: c# asp.net .net ajax pdfsharp


【解决方案1】:

看来你保存的是一个空的pdf,我认为这是你的问题,你的html参数必须有一些内容才能在下面的代码中生成pdf:

【讨论】:

    【解决方案2】:

    您是否检查过您指定的文件夹是否具有 IUSR/IIS 的 R/W 权限? 我会放一个断点并精确调试这一行:

    pdf.Save(Server.MapPath("~/Dumps/") + filename);
    

    【讨论】:

      猜你喜欢
      • 2012-01-01
      • 2017-05-03
      • 2011-11-28
      • 2022-01-06
      • 2022-07-18
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 2021-04-07
      相关资源
      最近更新 更多