I am attempting to download an SSRS report from a MVC C# application. I have an HttpResponseBase
object and am sending it back to the client to be displayed/downloaded by the user, however, when the response is sent back to the client, I am getting symbols only.
Can some one please help clarify what I am missing upon the success of the ajax call?
I have attempted to use an option which worked with some success, however, the URL is too long when selecting certain parameters.
My controller is as follows:
[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);
}
My view is as follows:
<div id="target"></div>
My javascript (ajax call) is as follows:
<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>
I am expecting either a downloaded version of the report or a prompt to Open (inline) or Save As to appear once the call is completed.
I am currently experiencing the following to be displayed:
%PDF-1.3 1 0 obj [/PDF /Text /ImageB /ImageC /ImageI] endobj 5 0 obj << /Length 1968 /Filter /FlateDecode >> stream x��[]o7}G��DZ��{�7��REi�H�Z�!�R�]�I������kdz��i#D���{αϽ���9��7h��|5�SM6�Y��Ҍt�t�J���4�DkC�$����9������.{�����y�� =����'�������Q�j ���]��]^����E���gG�^M�c�ʨ��/�A(�<�����a��"�>y�����b/��Ś�