0

好吧,我已经尝试了很多东西来解决这个问题,但没有一个。

我开发了一个报告服务(2005)并部署了它。

这份报告将被每个访问在框架 3.5 上开发的网站(它是一个互联网站点,因此不会被 Intranet 访问)的人使用(但我认为框架的版本不是问题的根源)。

我在身份验证方面遇到了其他问题,解决方法包括在我的网站上使用 FileStream 类。

ReportExecutionService rs = new ReportExecutionService();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
rs.Url = "http://MyServer/ReportServer/ReportExecution2005.asmx";

arguments
byte[] result = null;
string reportPath = "/ReportLuiza/ReportContract";
string format = "PDF";

// Prepare report parameter.
ParameterValue[] parameters = new ParameterValue[1];
parameters[0] = new ParameterValue();
parameters[0].Name = "NMB_CONTRACT";
parameters[0].Value = txtNmbContractReport.Text;

string encoding;
string mimeType;
string extension;
Warning[] warnings = null;
string[] streamIDs = null;

ExecutionInfo execInfo = new ExecutionInfo();
ExecutionHeader execHeader = new ExecutionHeader();

rs.ExecutionHeaderValue = execHeader;

execInfo = rs.LoadReport(reportPath, null);

rs.SetExecutionParameters(parameters, "pt-br");
String SessionId = rs.ExecutionHeaderValue.ExecutionID;

try
{
   result = rs.Render(format, null, out extension, out encoding, out mimeType, out warnings, out streamIDs);

   execInfo = rs.GetExecutionInfo();
}
catch (SoapException se)
{
    ShowMessage(se.Detail.OuterXml);
}

// Write the contents of the report to an pdf file.
try
{
    using (FileStream stream = new FileStream(@"c:\report.pdf", FileMode.Create, FileAccess.ReadWrite))
    {
        stream.Write(result, 0, result.Length);
        stream.Close();
    }
}
catch (Exception ex)
{
    ShowMessage(ex.Message);
}

对于此代码,我必须将 WebReference 添加到其中提到的 .asmx 文件。

报表和网站都部署/发布在具有 IIS 7.5 版本的同一台服务器上。

有没有一种方法可以让用户选择保存 .pdf 文件的位置?

任何帮助将不胜感激。

如果您需要更多信息来帮助我,请询问。

提前致谢。

4

1 回答 1

1

您可能想要组合这两个try-catch块:

ReportExecutionService rs = new ReportExecutionService();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
rs.Url = "http://MyServer/ReportServer/ReportExecution2005.asmx";

arguments
byte[] result = null;
string reportPath = "/ReportLuiza/ReportContract";
string format = "PDF";

// Prepare report parameter.
ParameterValue[] parameters = new ParameterValue[1];
parameters[0] = new ParameterValue();
parameters[0].Name = "NMB_CONTRACT";
parameters[0].Value = txtNmbContractReport.Text;

string encoding;
string mimeType;
string extension;
Warning[] warnings = null;
string[] streamIDs = null;

ExecutionInfo execInfo = new ExecutionInfo();
ExecutionHeader execHeader = new ExecutionHeader();

rs.ExecutionHeaderValue = execHeader;

execInfo = rs.LoadReport(reportPath, null);

rs.SetExecutionParameters(parameters, "pt-br");
String SessionId = rs.ExecutionHeaderValue.ExecutionID;

try
{
   result = rs.Render(format, null, out extension, out encoding, out mimeType, out warnings, out streamIDs);

   execInfo = rs.GetExecutionInfo();
}
catch (SoapException se)
{
    ShowMessage(se.Detail.OuterXml);
}

// Write the contents of the report to an pdf file.
try
{
    /*
    using (FileStream stream = new FileStream(@"c:\report.pdf", FileMode.Create, FileAccess.ReadWrite))
    {
        stream.Write(result, 0, result.Length);
        stream.Close();
    }
    */
    Response.Clear();
    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-Disposition", "attachment;filename=\"report.pdf\"");
    Response.BinaryWrite(result);
    Response.Flush();
    Response.End();
}
catch (Exception ex)
{
    ShowMessage(ex.Message);
}
于 2013-04-29T18:53:08.270 回答