2013-04-29 167 views
0

嗯,我已经尝试了很多东西来解决这个问题,但没有。下载FileStream对象无法打开下载对话框

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

这个报告将被所有访问一个网站(这是一个互联网网站,所以不会被intranet访问)在框架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); 
} 

对于这个代码,我只好一个Web引用添加到它提到的.asmx文件。

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

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

任何帮助将不胜感激。

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

在此先感谢。

+0

从我明白这个代码不提供对用户的报告都没有。相反,只能在服务器上使用所有内容,并且有权访问服务器C驱动器的用户可以打开report.pdf。 – Abhinav 2013-04-29 18:49:28

回答

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); 
} 
+0

谢谢@Abhinav。你救了我的生命,我的工作,我的母亲,我的狗和我的鹦鹉。 – Fernando 2013-04-29 19:17:24

+0

我爱鹦鹉!!! – Abhinav 2013-04-29 19:33:27