1

I have a controller action which generates and streams a PDF to the client, but would also like to have a controller action which emails the output of that PDF download action as an attachment. I know how to send an email, the question is how can I use/capture that MVC download action for my email attachment.

Pseudo code:

public PdfResult Download(int? someId)
{
     var pdfBuilder = new pdfBuilder();
     var pdfStream = pdfBuilder.StreamPdf(someId);
     return new PdfResult("someId.pdf", "application/pdf", pdfStream);
}

public ActionResult Email(int? someId)
{
     var pdfStream = View("Download", someId);
     var attachment = new Attachment(pdfStream, "someId.pdf");

     //...send email code
}
3
  • 1
    If you do already have that PdfBuilder of yours, why don't you just call it again when sending the email...? And if you don't want to duplicate code, just refactor the pdf generation code into a third method. Commented Feb 7, 2011 at 18:26
  • What are you using for sending email and what does it need for an attachment. That is does it require a physical file or a filestream? Commented Feb 7, 2011 at 18:48
  • Just SmptClient, typical .Net send email mechanism. Commented Feb 7, 2011 at 19:02

1 Answer 1

1

You might try to add a helper class to return the PdfResult instead of calling Download() in your Email() method.

Sign up to request clarification or add additional context in comments.

2 Comments

I like to be able to just use the same output of that download action since it already is binary.
You would call the helper from both methods. That would better adhere to the MVC pattern. You would receive the same output for both calls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.