我正在尝试使用Reporting Services 2005 webservice生成pdf。我有pdf生成部分工作,但我不知道如何获得一个“物理”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 :(得分: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();
答案 1 :(得分:2)
从MemoryStream
字节数组创建results
(reference),然后将该流添加到带ContentType
的附件中,并将附件添加到Attachments
1 {} 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);
}