2011-08-09 96 views
4

我正在嘗試使用Reporting Services 2005 webservice生成pdf。我有pdf生成部分工作,但我不知道如何得到一個「物理」pdf,我可以附加到一封電子郵件之前發送它的方式。通過web服務生成PDF並將其作爲附件通過電子郵件發送出去

我所創建的PDF此指南如下:http://www.codeproject.com/KB/reporting-services/PDFUsingSQLRepServices.aspx

public void RenderPdf(string rptMemo, string emailAddress) 
{ 
    // Prepare Render arguments 
    string historyID = null; 
    string deviceInfo = null; 
    string format = "PDF"; 
    Byte[] results; 
    string encoding = String.Empty; 
    string mimeType = String.Empty; 
    string extension = String.Empty; 
    Rse2005.Warning[] warnings = null; 
    string[] streamIDs = null; 

    //Credentials that will be passed to reporting services 
    Rse2005.ReportExecutionService rsExec = new Rse2005.ReportExecutionService(); 
    rsExec.Credentials = new NetworkCredential("username", "password", "domain"); 

    //Report is called "Report1", it takes a parameter called "Id" 
    Rse2005.ExecutionInfo ei = rsExec.LoadReport("/Reports/Report1", historyID); 
    Rse2005.ParameterValue[] rptParameters = new Rse2005.ParameterValue[1]; 

    rptParameters[0] = new Rse2005.ParameterValue(); 
    rptParameters[0].Name = "Id"; 
    rptParameters[0].Value = RptMemo; 

    //render the PDF 
    rsExec.SetExecutionParameters(rptParameters, "en-us"); 
    results = rsExec.Render(format, deviceInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs); 


    HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=DetailedQuote.pdf"); 
    HttpContext.Current.Response.OutputStream.Write(results, 0, results.Length); 

    //This is very important if you want to directly download from stream instead of file. 
    HttpContext.Current.Response.End(); 
} 

從這一點我能夠調用RenderPdf方法,我提示打開/保存/取消的文件。我知道如何發送帶有硬盤附件的電子郵件,但我不知道如何將結果[]轉換爲我可以處理的格式。

在此先感謝

+0

我有類似的SO看看http://stackoverflow.com/questions/919829/send-sql-report-in-e-mail-attachment-using-c-and-asp-net – adopilot

回答

2

您應該保存PDF文件到硬盤或內存中,然後使用System.Net.Mail將其發送 下面是谷歌的快速鏈接。 http://www.systemnetmail.com/faq/3.4.2.aspx

你可以寫從你的榜樣結果到內存流,像這樣

var memStream = new MemoryStream(); 
    memStream.Write(results, 0 , results.Length); 

你應該從你的代碼中刪除這3個行

HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=DetailedQuote.pdf"); 
    HttpContext.Current.Response.OutputStream.Write(results, 0, results.Length); 
    HttpContext.Current.Response.End(); 
+0

謝謝這正是我正在尋找的 – Curtis

2

創建MemoryStreamreference)從results字節數組,然後將該流添加到具有ContentType的附件中,並將附件添加到Attachments集合在MailMessage

using (MemoryStream ms = new MemoryStream(results, 0 , results.Length, false, true)) 
{ 
    MailMessage msg = new MailMessage(...); 

    ContentType ct = new ContentType() 
    { 
     MediaType = MediaTypeNames.Application.Octet, 
     Name = "DetailedQuote.pdf" 
    }; 

    Attachment att = new Attachment(ms, ct); 
    msg.Attachments.Add(att); 
} 
相關問題