从MVC C#应用程序中的Ajax调用显示或下载文档

问题描述 投票:0回答:1

我正在尝试从MVC C#应用程序下载SSRS报告。我有一个HttpResponseBase对象,并将其发送回客户端以供用户显示/下载,但是,当响应发送回客户端时,我仅获得符号。

有人可以帮忙澄清一下ajax调用成功后我缺少了什么吗?

我试图使用一个可以成功使用的选项,但是,在选择某些参数时,URL太长。

我的控制器如下:

[HttpPost]
public ActionResult DownloadReportData(string reportName, string reportPath, string reportParams, string repID, string book, string exportFormat)
        {
            string extension = string.Empty;
            string fileName = reportName + "_" + (book.Length > 0 ? "" + book + "_" : "") + repID;

            Dictionary<string, string> dc = new Dictionary<string, string>();
            if (reportParams.Length > 0)
            {
                string[] param = reportParams.Split(';');
                int i = 0;
                while (i < param.Length)
                {
                    dc.Add(param[i].Split('=')[0], param[i].Split('=')[1]);
                    i += 1;
                }
            }

            //PDF for pdf export, EXCEL for excel export
            byte[] bytes = autil.Reporting.ReportsManager.GenerateReport(reportPath, dc, exportFormat);

            Response.ClearContent();

            if (exportFormat.ToLower() == "pdf")
            {
                Response.ContentType = "application/pdf";
                extension = ".pdf";
                //Response.AppendHeader("Content-Disposition", "inline;filename=" + fileName + "" + extension + "");
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + "" + extension + "");
            }
            else if (exportFormat.ToLower() == "excel")
            {
                //Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.ContentType = "application/vnd.ms-excel";
                extension = ".xls";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + "");

            }

            //Response.AppendHeader("Content-Disposition", "inline;filename=" + fileName + "" + extension + "");
            Response.BufferOutput = true;
            Response.AddHeader("Content-Length", bytes.Length.ToString());
            Response.BinaryWrite(bytes);
            Response.End();

            //return null;
            return Json(Response);
        }

我的看法如下:

<div id="target"></div>

我的JavaScript(ajax调用)如下:

<script>
 $.ajax({
            type: 'POST',
            url: '@Url.Action("DownloadReportData", "Reports")',
            data:
            {
                reportName: reportName,
                reportPath: reportPath,
                reportParams: reportParams,
                book: bookCode,
                exportFormat: exportType
            }
            , success: function (data) {
                $("#target").append($("<iframe>", {
                    srcdoc: data
                }));                

                if (data.error == "none") {
                    alert("success" + data);
                }
            }


        });

</script>

我希望在呼叫完成后会出现报告的下载版本或打开(内联)或另存为的提示。

我目前正在显示以下内容:

%PDF-1.3 1 0 obj [/ PDF / Text / ImageB / ImageC / ImageI] endobj 5 0 obj << / Length 1968 / Filter / FlateDecode >>流x��[] o7}G��DZ�� {�7�REi�H�Z�!�R�]�I������kdz��i#D�{αϽ���9��7h��|5�SM6�Y ��t�t�J���4�DkC�$����9������。{����yy= =����'������� Q�j���]��] ^����E�gg�^M�c�ʨ��/�A(�y��� ��b/��Ś�

c# ajax asp.net-mvc httpresponse
1个回答
0
投票

当我尝试从ajax调用中下载excel时遇到了同样的问题。许多天后,我得到了您无法回答的信息(我在SO上的某个地方阅读了该信息)。因此我将ajax调用转换为此

<div class="col-md-3">
      @Html.ActionLink("Export Report", "ExportExcel", "Training",
                        new { }, new { @class = "btn btn-danger" })
</div>

之后,我的代码开始工作了。您可能必须进行一些更改才能使其正常运行。希望对您有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.