2013-04-29 72 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