简体   繁体   English

将报告从报告查看器导出到内存流,然后通过电子邮件发送报告

[英]Export report from report viewer to memory stream and then email report

I currently have a page which displays a local report (which takes parameters in a report viewer control on my webpage. I have a requirement were the user wants the report to be generated and then emailed via a button click or page load. I have the below code which I have used to try and load the report into a memory stream and then to be emailed. The email part works fine but the pdf doesn't generate. What is the best way to load the pdf and email it since it isn't a physical file as such? 我目前有一个显示本地报告的页面(该页面在我的网页上的报告查看器控件中使用参数。我有一个要求,就是用户希望生成报告,然后通过单击按钮或页面加载方式通过电子邮件发送该报告。下面的代码是我用来尝试将报告加载到内存流中然后通过电子邮件发送的代码:电子邮件部分可以正常工作,但不会生成pdf文件。加载pdf并通过电子邮件发送邮件的最佳方法是什么这样的物理文件吗?

    WebClient client = new WebClient();
        byte[] bytes = client.DownloadData("http://localhost:51997/ReportDetails.aspx?Report1.rdlc&rs%3aFormat=PDF");

        MemoryStream ms = new MemoryStream(bytes);

        MailMessage mailObj = new MailMessage("FromAddress", "ToAddress", "header", "body text");

        SmtpClient SMTPServer = new SmtpClient("RelayServer");
        mailObj.IsBodyHtml = true;
        mailObj.Attachments.Add(new Attachment(ms, "Reports.pdf"));
        try
        {
            SMTPServer.Send(mailObj);
        }
        catch (Exception)
        {
            throw;
        }

I worked this out by loading the local report into a byte array, writing to a file stream and then loading it into memory stream where I could then email. 我通过将本地报告加载到字节数组中,写入文件流,然后将其加载到内存流(然后可以通过电子邮件发送)来解决此问题。

  Warning[] warnings;
        string[] streamids;
        string mimeType;
        string encoding;
        string filenameExtension;

        byte[] bytes = ReportViewer1.LocalReport.Render(
           "PDF", null, out mimeType, out encoding, out filenameExtension,
            out streamids, out warnings);


        string filename = Path.Combine(Path.GetTempPath(), "Report2.rdlc");

        using (var fs = new FileStream(filename, FileMode.Create))
        {
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
        }

Load into memory stream: 加载到内存流中:

MemoryStream ms = new MemoryStream(bytes);

Then send as an attachment: 然后作为附件发送:

  MailMessage mailObj = new MailMessage("From", "To", "header", "body");

        SmtpClient SMTPServer = new SmtpClient("relayServer");
        mailObj.IsBodyHtml = true;

        mailObj.Attachments.Add(new Attachment(ms, "report.pdf"));

        try
        {
            SMTPServer.Send(mailObj);
        }
        catch (Exception)
        {
            throw;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM