0

This will send an email when a button gets pushed. However I am trying to call the FileResult, SaveDocument, to download a file right before redirecting back to the button page.

I am using a hardcoded file for now to download for the sake of testing. I can run the SaveDocument() result using a test button. I can't send an email, run the SaveDocument Action and then redirect.

[HttpGet]
public ActionResult send(int thisbatch, string listofpositives)
{
    MailMessage mail = new MailMessage();

    SmtpClient smtpServer = new SmtpClient("smtperServer");
    smtpServer.Port = 25; // Gmail works on this port
    smtpServer.EnableSsl = false;
    mail.From = new MailAddress("xxxe@xxx.com");
    mail.To.Add("xxxe@xxx.com");
    mail.Subject = "Batch Closed";
    mail.Body="some info stuff here";

    smtpServer.Send(mail);

    SaveDocument();

    return RedirectToAction("AddColiform");

}

//THIS WORKS BY ITSELF CALLED FROM A BUTTON
    public FileResult SaveDocument()
    {
        string filePath = Server.MapPath("~/XML_positives/Test1.xml");
        string contentType = "text/xml";

        return File(filePath, contentType, "Test1.xml");

    }
JustJohn
  • 1,362
  • 2
  • 22
  • 44
  • if you want to download only file call SaveDocument after redirect in AddColiform – Light Jul 18 '15 at 11:18
  • So are you saying put the "SaveDocument()" call at the beginning of "AddColiform" ActionResult? I will have to send a flag because most of the time there is no reason to "SaveDocument()". Only if sample is a detect. I will try that but today I am off work. stand by thanks. – JustJohn Jul 18 '15 at 19:05
  • I can't call SaveDocument after redirect in AddColiform. Once redirect is called, nothing can run afterwards. I tried it. I tried putting it at the top of AddColiform, that doesn't work either. – JustJohn Jul 20 '15 at 17:30

1 Answers1

0

Well, no solution was found (so far) to download a file and RedirectToAction back to the initial page in the same ActionResult. If someone can come up with a better answer I will take this one off.

So based on contents of string "listofpositives" I call a new View "Has Positives" with 2 buttons: One calls the FileResult Action and one redirects back to where everything started (this is desired). A lot clunkier than just popping up a File Save As dialog and then moving on automatically. But I need to build something and move on. I feel sorry for my users. oh well.

Here is the code after I send an email and return to desired view:

if (listofpositives == "")
{

    return RedirectToAction("AddColiform");
}
else
{
    return RedirectToAction("HasPositives",new { thisbatch=thisbatch, listofpositives=listofpositives});
}

Here is the code for the whole extra view:

@{
    ViewBag.Title ="HasPositives" ; 
}

<h2>HasPositives</h2>


<p>Batch: @ViewData["batchid"] </p>
<p>Postives: @ViewData["listofpositives"]</p>

<br /><br />

Process your XML file using XMLSampling<br /><br /><br />

&nbsp;&nbsp;&nbsp;&nbsp;

<button onclick="location.href='@Url.Action("SaveDocument", "Home")';return false;">Download Positive XML File</button>

&nbsp;&nbsp;&nbsp;&nbsp;
<button onclick="location.href='@Url.Action("AddColiform", "Home")';return false;">Done</button>
JustJohn
  • 1,362
  • 2
  • 22
  • 44