【问题标题】:SSRS -Image not rendering on HTML 4.0 Report(Unauthorization )SSRS - 图像未在 HTML 4.0 报告上呈现(未授权)
【发布时间】:2017-11-10 09:46:36
【问题描述】:

我正在处理 SSRS 报告。我正在使用网络服务访问报告(使用网络参考) 我正在使用 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,静止图像不显示在报告上 任何人都可以为此或任何相关代码提供解决方案

提前致谢

【问题讨论】:

    标签: reporting-services ssrs-2012


    【解决方案1】:

    这几乎是正确的。您缺少一小部分。

    您不仅需要拦截流并将每个流保存到您的应用可以访问的临时位置,还必须让 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. 截获报告流并保存到上述步骤中指定的可访问位置。

    注意:

    以下是一些从 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\";
    

    【讨论】:

    • 嗨,我正在将“devInfo.ToString()”传递给我的渲染函数,但它会引发异常。那个“无效的设备信息”,我认为是因为错误的物理网络根和虚拟网络根..请您详细说明“YourPhysicalWebRoot”和“YourVirtualWebRoot”应该是什么。
    • 我添加了获取这些文件夹的方式。
    • 我像这样更改了设备信息标签-True 并且它工作正常.. 图像在 html 上呈现...我有相同的图像所有报告都可以使用相同的图像来替换 evry 报告...evrtime.it 将新的 imagd 保存在文件夹中...有没有办法使用 single.image 进行报告调用...
    • 我很高兴它能正常工作。至于使用相同的图像......因为你正在拦截streamroot,我想你可以尝试识别多个报告使用的图像,并且只在第一次找不到它时保存它,但是,你可能会遇到问题路径。您只能为报告指定一个位置,所以我不知道您将如何为每个报告指定一个位置以及一个共享位置。但是,您几乎必须将所有报告呈现到一个临时文件夹中,然后您可能会在那里遇到冲突。看看你是否能破解并让它发挥作用会很有趣。
    • 在需要将报表内容注入到 div 标签等的情况下,您会使用 HTMLFragment。它移除了头部和身体元素,并很好地适应了现有网页。
    【解决方案2】:

    下面是我使用报告服务(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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-24
      相关资源
      最近更新 更多