问题
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;
}
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
Thanks In Advance
回答1:
This is close to being correct. There is one small part you are missing.
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.
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);
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.
EDIT :
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:
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.
NOTE :
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\";
回答2:
Below isway I achieve render report using reporting services (ReportExecutionSerivce class and webrefrence of SSRS)
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;
}
and here I pass required parameters to class
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);
来源:https://stackoverflow.com/questions/47219831/ssrs-image-not-rendering-on-html-4-0-reportunauthorization