下载FileStream对象不会打开“下载”对话框

时间:2013-04-29 18:39:45

标签: c# asp.net filestream reportingservices-2005

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

我开发了一个Reporting Service(2005)并部署了它。

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

我在身份验证方面遇到了其他问题,解决方法包括在我的网站上使用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文件中。

报告和WebSite都在IIS 7.5版本的同一台服务器上部署/发布。

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

任何帮助将不胜感激。

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

提前致谢。

1 个答案:

答案 0 :(得分: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);
}