从 AJAX post 调用时 FileContentResult 不生成文件

FileContentResult not generating file when called from AJAX post

如果这有点含糊,我深表歉意,但问题本身并没有暴露太多。我一直在编写一个使用 MigraDoc 生成 PDF 的应用程序。用于生成下载PDF的controller方法如下:

    public FileContentResult ConvertToPDF(int reportTypeId, string cultureName, int headerFooterTemplateId, int baseClassId, string baseTypeName)
    {
        try
        {
            string resourcePath = @"C:\TFS\Products\EnGenero\Trunk\EnGenero Application\RiskNetResources\bin\Debug\RiskNetResources.dll"; // --<<-- Reference this

            byte[] result = new DocumentWriter().ConvertDocumentToPDFSharp(GetSeedData(reportTypeId, cultureName, resourcePath, headerFooterTemplateId, baseClassId));

            return new FileContentResult(result, "application/pdf")
            {
                FileDownloadName = "MyReportFile.pdf"
            };
        }
        catch (Exception ex)
        {
            Logger.Instance.LogError("Error in ConvertToPDF", ex);
            return new FileContentResult(GetBytes("Error fetching pdf, " + ex.Message + Environment.NewLine + ex.StackTrace), "text/plain");
        }
    }

在开发过程中,这运行良好,当点击上面的代码时,可以通过浏览器正常下载 PDF。在开发过程中,我直接从带有硬编码参数的 JQuery 对话框调用此控制器方法。

但是,我进一步开发了该应用程序,现在通过局部视图中的 Ajax Post 调用此操作方法。

function CreateDocumentPDF() {

    var baseClassId = @Html.Raw(Json.Encode(ViewData["baseClassId"]));
    var baseTypeName = @Html.Raw(Json.Encode(ViewData["baseTypeName"]));
    var reportTypeId = $j('#ddlReportType option:selected').attr('Value');
    var branchId = $j('#ddlBranch option:selected').attr('Value');
    var languageId = $j('#ddlLanguage option:selected').attr('Value');

    $j.ajax({
        url: appRoot + 'DocumentPDFPrinter/ConvertToPDF',
        type: 'post',
        data: { 'reportTypeId': reportTypeId, 'cultureName': languageId, 'headerFooterTemplateId': branchId, 'baseClassId': baseClassId, 'baseTypeName': baseTypeName },
        success: function (data) {
            closeDefaultPopup();
        },
        failure: function () {
            alert("Error Generating PDF.");
        }
    });
}

传递完全相同的参数值并且控制器操作按预期运行,但是现在没有文件 generated/downloaded。

我只能想象这与 Ajax post 有关,因为这是我所看到的 运行 好与不好的唯一区别。

这是我得到的回复 - 就我所见,看起来还不错...?我错过了什么吗?

所以我只是离开了 AJAX 调用,而是现在调用:

    window.location = appRoot + "DocumentPDFPrinter/ConvertToPDF?reportTypeId=" + reportTypeId + "&cultureName=" + languageId etc...

这似乎做得很好。