简体   繁体   English

从AJAX帖子中调用时,FileContentResult不生成文件

[英]FileContentResult not generating file when called from AJAX post

I apologize if this is a little vague, however the issue presenting itself isnt giving too much away. 如果这有点含糊,我深表歉意,但是提出的问题本身并没有给我们太多的余地。 I have been writting an application that uses MigraDoc to generate PDFs. 我一直在编写一个使用MigraDoc生成PDF的应用程序。 The controller method used to generate and download the PDF is as follows: 用于生成和下载PDF的控制器方法如下:

    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");
        }
    }

During development this has worked fine and when the above code is hit the PDF downloads through the browser fine. 在开发过程中,此方法运行良好,当单击上述代码时,可以通过浏览器下载PDF。 During development I was calling this controller method directly from a JQuery dialog box with Hard Coded parameters. 在开发过程中,我直接从带有硬编码参数的JQuery对话框中调用此控制器方法。

However, I further developed the application and now call this action method through an Ajax Post in a partial view. 但是,我进一步开发了该应用程序,现在通过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.");
        }
    });
}

The same exact same parameter values are passed and the controller action runs through as expected however now there is no file generated/downloaded. 传递完全相同的相同参数值,并且控制器操作按预期运行,但是现在没有文件生成/下载。

I can only imagine it is something to do with the Ajax post as this is the only difference between it running fine or not from what I can see. 我只能想象这与Ajax帖子有关,因为从我所见,这是它运行正常与否的唯一区别。

This is the response I am getting - looks okay as far as I can see...? 这是我得到的回应-就我所知看起来还不错...? Am I missing anything? 我有什么想念的吗? 在此处输入图片说明

So I simply moved away from the AJAX call and instead am now calling: 因此,我只是放弃了AJAX调用,而是现在打电话给:

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

Which seems to do the job nicely. 这似乎做得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM