简体   繁体   中英

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. The controller method used to generate and download the PDF is as follows:

    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. During development I was calling this controller method directly from a JQuery dialog box with Hard Coded parameters.

However, I further developed the application and now call this action method through an Ajax Post in a partial view.

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.

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:

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

Which seems to do the job nicely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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