本文介绍了SSRS - 图像未在 HTML 4.0 报告上呈现(未授权)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on SSRS report. I am accessing report using web services (using web refrences)I am using ReportExecutionService class to render report in the html 4.0 format and finaly I attach rendered HTML to my page DIV. Report is rendering very fine in HTML format but the images on that is not rendering properly because of missing authentication for images for that I just replace the src attribute of the img tag in the response returned from the SSRS report execution service with the url to this location below is code for render report:-

 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;

    }

和用于图像替换的函数(代码)

and function (code) for image replacement

   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,报告中不显示静止图像任何人都可以为此或任何相关代码提供解决方案

but issue is that image is saved into folder and also src tag is replaced into html ,still image is not display on report Can anybody have solution for this or any related code for same

提前致谢

推荐答案

这几乎是正确的.你遗漏了一小部分.

This is close to being correct. There is one small part you are missing.

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

Not only do you have to intercept the streams and save each to a temp location that is accessible to your app, you also have to let SSRS know that the renderer should source the dynamic images to your new location.

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

The way to let the renderer know about the new location is to override the DeviceInfo.StreamRoot passed into the Render() method.

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[] 应该包含您的报告,并且一旦它显示在网页上,嵌入的图像现在应该来自您的网络应用程序可以访问的临时位置.

After the render, the byte[] should contain your report and once it has been displayed on a web page the embedded images should now be sourced to your temp location that is accessible to your web app.

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

Also, I noticed that you are trying to do some sort of post processing to the content that is returned from Render(). You do not have to touch that at all. Since you told SSRS to replace the image sources via the StreamRoot property then the renderer can handle it from there.

重新路由报告资产所需的唯一步骤:

The only steps needed to re-path your report's assets:

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

  1. Let SSRS know where you plan to place the assets it will be referencing in the report.

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

Intercept the report streams and save to an accessible location specified in the step above.

注意:

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

Here are some ways to get a virtual and physical temp folder from the context of your web app..

//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\";

这篇关于SSRS - 图像未在 HTML 4.0 报告上呈现(未授权)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:46