以PDF格式打开Crystal报表时更新浏览器标题

时间:2018-02-08 17:31:58

标签: c# asp.net-mvc-4 pdf crystal-reports

我正在使用MVC 4开发Web应用程序。对于报告,我使用的是Crystal Report。我的客户希望所有PDF格式的报告和报告都应在浏览器的新标签页中打开。

但是在我在新标签页面中生成报告后,标签标题是控制器名称。我想将报告名称设置为浏览器标题。我怎样才能做到这一点? 下面是我生成水晶报告的代码:

public class CrystalReportResult:ActionResult
{

    private readonly byte[] _contentBytes;


    public CrystalReportResult(string reportPath, object dataSet,List<ReportParameter> paramList,string type="pdf")
    {
        ReportDocument reportDocument = new ReportDocument();
        reportDocument.Load(reportPath);

        reportDocument.SetDataSource(dataSet);
        foreach (var item in paramList)
        {
            reportDocument.SetParameterValue(item.Name, item.Value);
        }

        #region extra
        //ParameterValues currentParameterValues = new ParameterValues();
        //ParameterDiscreteValue parameterDiscreteValue = new ParameterDiscreteValue();
        //ParameterFieldDefinitions parameterFieldDefinitions = reportDocument.DataDefinition.ParameterFields;
        //ParameterFieldDefinition parameterFieldDefinition;



        //foreach (var param in paramList)
        //{
        //    if (param.Value != null)
        //    {
        //        if (param.IsInteger)
        //            var value = Int64.Parse(param.Value);
        //        else if (param.IsDateTime)
        //            parameterDiscreteValue.Value = DateTime.Parse(param.Value);
        //        else if (param.IsBoolean)
        //            parameterDiscreteValue.Value = bool.Parse(param.Value);
        //        else
        //            parameterDiscreteValue.Value = param.Value;
        //    }
        //    else
        //    {
        //        parameterDiscreteValue.Value = DBNull.Value;
        //    }

        //    currentParameterValues.Add(parameterDiscreteValue);

        //    parameterFieldDefinition = parameterFieldDefinitions[param.Name];
        //    parameterFieldDefinition.ApplyCurrentValues(currentParameterValues);

        //}
        #endregion

        _contentBytes = StreamToBytes(reportDocument.ExportToStream(GetFormatType(type)));
    }


    public override void ExecuteResult(ControllerContext context)
    {

        var response = context.HttpContext.ApplicationInstance.Response;
        response.Clear();
        response.Buffer = false;
        response.ClearContent();
        response.ClearHeaders();
        response.Cache.SetCacheability(HttpCacheability.Public);
        response.ContentType = "application/pdf";

        var headerValue = "Test.pdf";
        context.HttpContext.Response.AddHeader(
                        "Content-Disposition", "inline; filename: " + headerValue);

        using (var stream = new MemoryStream(_contentBytes))
        {
            stream.WriteTo(response.OutputStream);
            stream.Flush();
        }
    }

    private static byte[] StreamToBytes(Stream input)
    {
        byte[] buffer = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }

    private ExportFormatType GetFormatType(string type) 
    {
        if (type=="pdf")
        {
            return ExportFormatType.PortableDocFormat;
        }
        else
        {
            return ExportFormatType.PortableDocFormat;
        }            
    }
}

提前致谢。

0 个答案:

没有答案