SSRS -Image不在HTML 4.0报表上呈现(未授权)

时间:2017-11-10 09:46:36

标签: reporting-services ssrs-2012

我正在研究SSRS报告。我正在使用Web服务访问报告(使用Web引用) 我正在使用ReportExecutionService类以html 4.0格式呈现报表,最后我将呈现的HTML附加到我的页面DIV。报告在HTML格式中呈现得非常好,但由于缺少对图像的身份验证,其上的图像无法正确呈现  为此,我只是将SSRS报告执行服务返回的响应中的img标记的src属性替换为下面这个位置的url是渲染报告的代码: -

 public string Render(string reportDirectory,string reportName,string reportFormat, ParameterValue[]parameters )
        {
            _reportServerExecutionService.ExecutionHeaderValue = new ExecutionHeader();
            _reportServerExecutionService.TrustedUserHeaderValue = new TrustedUserHeader();
            _reportServerExecutionService.LoadReport("/GES-MVC/GES_FWCR",null);
            _reportServerExecutionService.SetExecutionParameters(parameters, "en-us");


            string encoding;
            string mimeType;
            string extension;
            Warning[] warnings;
            string[] streamIds;

            var result = _reportServerExecutionService.Render(reportFormat, @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>", out extension, out encoding, out mimeType, out warnings, out streamIds);
            //Here is logic for image replcaement

            string html = string.Empty;
            html = System.Text.Encoding.Default.GetString(result);
            html = GetReportImages(_reportServerExecutionService, _reportServerExecutionService.ExecutionHeaderValue, _reportServerExecutionService.TrustedUserHeaderValue, reportFormat, streamIds, html);


        return html;

    } 

和用于图像替换的功能(代码)

   public string GetReportImages(ReportExecutionService _reportServerExecutionService, ExecutionHeader executionHeaderValue, TrustedUserHeader trustedUserHeaderValue, string reportFormat, string[] streamIds, string html)
        {

                if (reportFormat.Equals("HTML4.0") && streamIds.Length > 0)
                {
                    string devInfo;
                    string mimeType;
                    string Encoding;
                    string fileExtension = ".jpg";
                    string SessionId;
                    Byte[] image;

                    foreach (string streamId in streamIds)
                    {
                        SessionId = Guid.NewGuid().ToString().Replace("}", "").Replace("{", "").Replace("-", "");


                        string reportreplacementname = string.Concat(streamId, "_", SessionId, fileExtension);
                        html = html.Replace(streamId, string.Concat(@"Report\GraphFiles\", reportreplacementname));
                        devInfo = "";

                   image= _reportServerExecutionService.RenderStream(reportFormat, streamId, devInfo, out Encoding, out mimeType);

                    System.IO.FileStream stream = System.IO.File.OpenWrite(HttpContext.Current.Request.PhysicalApplicationPath + "Report\\GraphFiles\\" + reportreplacementname);
                    stream.Write(image, 0, image.Length);
                    stream.Close();
                        mimeType = "text/html";
                    }



            }

            return html;


        }

但问题是图像被保存到文件夹中,并且src标签被替换为html,静止图像不会显示在报告上任何人都可以为此或任何相关代码提供相同的解决方案

提前致谢

2 个答案:

答案 0 :(得分:0)

这接近正确。你缺少一小部分。

您不仅要拦截流并将每个流保存到应用可访问的临时位置,还必须让SSRS知道渲染器应该将动态图像发送到您的新位置。

让渲染器知道新位置的方法是覆盖传递给Render()方法的 DeviceInfo.StreamRoot

string thisReportInstanceID = Guid.NewGuid();
string thisReportInstanceTempFolder = Path.Combine("Temp",thisReportInstanceID );
string physicalTempFolder = Path.Combine(<YourPhysicalWebRoot>,thisReportInstanceTempFolder );
string virtualTempFolder =<YourVirtualWebRoot>+"/Temp/"+thisReportInstanceID );

Directory.Create(physicalTempFolder );

StringBuilder devInfo = new StringBuilder();
if (format.ToUpper().StartsWith("HTML"))
{
    devInfo.Append("<DeviceInfo>");
    //StreamRoot should be your fully qualified domain name + temp folder for this report instance.
    devInfo.Append("<StreamRoot>" + virtualTempFolder+ "</StreamRoot>");                
    devInfo.Append("</DeviceInfo>");                
}

var result = _reportServerExecutionService.Render(reportFormat, devInfo.ToString(),out extension, out encoding, out mimeType, out warnings, out streamIds);

渲染后,byte[]应包含您的报告,一旦在网页上显示,嵌入的图像现在应该来源于您的网络应用可访问的临时位置。

编辑:

另外,我注意到您正在尝试对从Render().返回的内容进行某种后期处理。您根本不需要触摸它。由于您告诉SSRS通过StreamRoot属性替换图像源,因此渲染器可以从那里处理它。

重新处理报告资产所需的唯一步骤:

  1. 让SSRS知道您计划在报告中放置它将引用的资产。

  2. 拦截报告流并保存到上述步骤中指定的可访问位置。

  3. 注意:

    以下是从Web应用程序的上下文中获取虚拟和物理临时文件夹的一些方法..

    //Virtual Temp Folder - MVC                
     System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + Url.Content("~/Temp/");
    //Virtual Temp Folder - Non-MVC
    VirtualPathUtility.ToAbsolute("~/Temp/"); 
    //Physical Temp Folder
    AppDomain.CurrentDomain.BaseDirectory + @"Temp\";
    

答案 1 :(得分:0)

以下是使用报告服务(ReportExecutionSerivce类和SSRS的webrefrence)实现渲染报告

 public class ReportManager
    {
        private readonly ReportExecutionService _reportServerExecutionService;

        public ReportManager(string reportServerWsdlUrl, string username, string password, string domain)
        {
            _reportServerExecutionService = new ReportExecutionService
            {
                Url = reportServerWsdlUrl,
                Credentials = new NetworkCredential(username, password)
            };
        }

        public string Render(string reportDirectory, string reportName, string reportFormat, ParameterValue[] parameters)
        {
            _reportServerExecutionService.ExecutionHeaderValue = new ExecutionHeader();
            _reportServerExecutionService.LoadReport("/GES-MVC/"+reportName, null);
            _reportServerExecutionService.SetExecutionParameters(parameters, "en-us");


            string encoding;
            string mimeType;
            string extension;
            Warning[] warnings;
            string[] streamIds;


            var result = _reportServerExecutionService.Render(reportFormat, @"<DeviceInfo><StreamRoot>/</StreamRoot><HTMLFragment>True</HTMLFragment></DeviceInfo>", out extension, out encoding, out mimeType, out warnings, out streamIds);
            //Here is logic for image replcaement


            string html = string.Empty;
            html = System.Text.Encoding.Default.GetString(result);
            html = GetReportImages(_reportServerExecutionService, reportFormat, streamIds, html);


            return html;

        }

        public string GetReportImages(ReportExecutionService _reportServerExecutionService, string reportFormat, string[] streamIds, string html)
        {

            if (reportFormat.Equals("HTML4.0") && streamIds.Length > 0)
            {
                string devInfo;
                string mimeType;
                string Encoding;
                string fileExtension = ".jpg";
                string SessionId;
                Byte[] image;



                foreach (string streamId in streamIds)
                {
                    SessionId = Guid.NewGuid().ToString().Replace("}", "").Replace("{", "").Replace("-", "");


                    string reportreplacementname = string.Concat(streamId, "_", SessionId, fileExtension);
                    html = html.Replace(streamId, string.Concat(@"Report\GraphFiles\", reportreplacementname));

                    devInfo = "";
                    image = _reportServerExecutionService.RenderStream(reportFormat, streamId, devInfo, out Encoding, out mimeType);


                    System.IO.FileStream stream = System.IO.File.OpenWrite(HttpContext.Current.Request.PhysicalApplicationPath + "Report\\GraphFiles\\" + reportreplacementname);
                    stream.Write(image, 0, image.Length);
                    stream.Close();
                    mimeType = "text/html";
                }



            }

            return html;


        }

在这里我将所需的参数传递给类

string rprwebserviceurl = exceservice;
                string ReportDirName = "GES-MVC";
                string ReportName = "GES_FWCR";
                string RptFormat = "HTML4.0";
                ParameterValue[] parameters = new ParameterValue[5];
                parameters[0] = new ParameterValue();
                parameters[0].Name = "PlantID";
                parameters[0].Value = PlantID;
                parameters[1] = new ParameterValue();
                parameters[1].Name = "FromDateTime";
                parameters[1].Value = Convert.ToString(fromDate); // June
                parameters[2] = new ParameterValue();
                parameters[2].Name = "ToDateTime";
                parameters[2].Value = Convert.ToString(Todate);
                parameters[3] = new ParameterValue();
                parameters[3].Name = "ClientID";
                parameters[3].Value = ClientID;
                parameters[4] = new ParameterValue();
                parameters[4].Name = "SelectedFeeders";
                parameters[4].Value = SelectedFeeders;
                ReportManager rpt = new ReportManager(rprwebserviceurl,RptUserName,RptPassword, "localhost");
                res = rpt.Render(ReportDirName, reportName, RptFormat, parameters);