SSRS 将在您指定的位置流式传输资源。
private byte[] InternalRenderReport(Report report, List<ReportParameter> parameters, string format, int pageNumber, ref int totalPages, string virtualTempFolder, string physicalTempFolder)
{
CheckConnection();
byte[] result = null;
ReportExecution2005.ReportExecutionService _execService=new ReportExecution2005.ReportExecutionService();
sys = _systemService.GetCurrentSystem();
_execService.Url = sys.ReportingServices.ServiceRootURL+"/ReportExecution2005.asmx";
NetworkCredential credentials = new NetworkCredential(sys.ReportingServices.Credentials.UserName,
sys.ReportingServices.Credentials.Password,
sys.ReportingServices.Credentials.Domain);
_execService.Credentials=credentials;
ReportExecution2005.ParameterValue[] rsParams = null;
if (parameters != null)
{
rsParams = new ReportExecution2005.ParameterValue[parameters.Count];
int x = 0;
foreach (ReportParameter p in parameters)
{
rsParams[x] = new ReportExecution2005.ParameterValue();
rsParams[x].Name = p.ParameterName;
rsParams[x].Value = p.SelectedValue;
x++;
}
}
StringBuilder devInfo = new StringBuilder();
if (format.ToUpper().StartsWith("HTML"))
{
devInfo.Append("<DeviceInfo>");
devInfo.Append("<HTMLFragment>True</HTMLFragment>");
devInfo.Append("<Section>" + pageNumber.ToString() +"</Section>");
devInfo.Append("<StreamRoot>" + virtualTempFolder + "</StreamRoot>");
/*devInfo.Append("<Zoom>200</Zoom>");*/
devInfo.Append("</DeviceInfo>");
}
else
devInfo.Append("<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>");
string extension;
string mimeType;
string encoding;
string[] streamIDs = null;
ReportExecution2005.Warning[] warnings = null;
ReportExecution2005.ExecutionHeader execHeader = new ReportExecution2005.ExecutionHeader();
ReportExecution2005.ExecutionInfo rpt = _execService.LoadReport(report.ReportPath, null);
if(rsParams!=null)
_execService.SetExecutionParameters(rsParams, "en-us");
_execService.ExecutionHeaderValue = execHeader;
_execService.ExecutionHeaderValue.ExecutionID = rpt.ExecutionID;
//result = _execService.Render2(format, devInfo, ReportExecution2005.PageCountMode.Actual, out extension, out mimeType, out encoding, out warnings, streamIDs);
result = _execService.Render(format, devInfo.ToString(), out extension, out mimeType, out encoding, out warnings, out streamIDs);
if (format.ToUpper().StartsWith("HTML"))
{
// For each image stream returned by the call to render,
// render the stream and save it to the application root
string FilePath = physicalTempFolder;
byte[] image;
// For each image stream returned by the call to render,
// render the stream and save it to the application root
foreach (string streamID in streamIDs)
{
image = _execService.RenderStream("HTML4.0", streamID, null, out encoding, out mimeType);
FileStream stream = File.OpenWrite(FilePath + streamID);
stream.Write(image, 0, image.Length);
stream.Close();
}
}
rpt = _execService.GetExecutionInfo();
totalPages = rpt.NumPages;
return result;
}
这将返回原始 HTML 或内容以推送文件。我在要部署到服务器的解决方案中添加了一个 Temp 文件夹。您可以在 temp 文件夹中放置一个 web.config 文件,其中包含以下内容,以允许扩展更少的内容,因为 ssrs 在使用流时将呈现:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".*" mimeType="image/png" />
</staticContent>
<handlers>
<clear />
<add name="StaticFile" path="*" verb="*" type="" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" scriptProcessor="" resourceType="Either" requireAccess="Read" allowPathInfo="false" preCondition="" responseBufferLimit="4194304" />
</handlers>
</system.webServer>
</configuration>
然后使用以下函数来获取物理和虚拟临时文件夹:
PhyscicalTempFolder= AppDomain.CurrentDomain.BaseDirectory + @"Temp\";
VirtualTempFolder=return Url.Content("~/Temp/");
最后要在每天之后进行清理,您可以添加类似于以下内容的 powershell 命令:
Remove-Item D:\xxx\WebApplications\ExternalReports\Temp\* -exclude *.config
然后添加一个调用 PS 脚本的 .bat:
powershell -command "& 'C:\xxx\Scripts\SSRSCleanTempFiles\SSRSCleanTempFiles.ps1'"
有了这个,您可以在服务器上配置一个计划任务来每天调用 .bat 文件并清理应用程序的临时文件夹。