首先运行FileResult下载文件,然后运行RedirectToAction

时间:2015-07-18 00:05:43

标签: asp.net-mvc-4 controller redirecttoaction fileresult

当按下按钮时,这将发送电子邮件。但是我试图在重定向回按钮页面之前调用FileResult,SaveDocument来下载文件。

我现在正在使用硬编码文件进行下载以便进行测试。我可以使用测试按钮运行SaveDocument()结果。我无法发送电子邮件,运行SaveDocument操作,然后重定向。

[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");

    }

1 个答案:

答案 0 :(得分:0)

好吧,到目前为止,没有找到将文件和RedirectToAction下载回同一ActionResult中的初始页面的解决方案。如果有人能提出更好的答案,我会把这一个拿走。

因此,基于字符串“listofpositives”的内容,我使用2个按钮调用新视图“Has Positives”:一个调用FileResult Action,一个重定向回所有内容开始(这是所需的)。 比弹出“文件另存为”对话框然后自动继续更加笨拙。但我需要建立一些东西并继续前进。我为我的用户感到难过。哦,好吧。

以下是我发送电子邮件并返回所需视图后的代码:

if (listofpositives == "")
{

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

以下是整个额外视图的代码:

@{
    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>