【问题标题】:First run FileResult to download file, then RedirectToAction首先运行 FileResult 来下载文件,然后是 RedirectToAction
【发布时间】:2015-10-07 19:13:00
【问题描述】:

这将在按下按钮时发送一封电子邮件。但是,我试图在重定向回按钮页面之前调用 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");

    }

【问题讨论】:

  • 如果你想在 AddColiform 重定向后只下载文件调用 SaveDocument
  • 那么您是说将“SaveDocument()”调用放在“AddColiform”ActionResult 的开头?我将不得不发送一个标志,因为大多数时候没有理由“SaveDocument()”。仅当样本是检测时。我会试试的,但今天我下班了。待命,谢谢。
  • 在 AddColiform 中重定向后无法调用 SaveDocument。一旦调用了重定向,之后就无法运行任何东西。我尝试过这个。我试着把它放在 AddColiform 的顶部,也不管用。

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


【解决方案1】:

嗯,(到目前为止)没有找到解决方案来下载文件并将 RedirectToAction 返回到同一 ActionResult 中的初始页面。如果有人能想出更好的答案,我会取消这个。

因此,根据字符串“listofpositives”的内容,我调用一个带有 2 个按钮的新视图“有阳性”:一个调用 FileResult 操作,一个重定向回一切开始的地方(这是需要的)。 比仅仅弹出一个文件另存为对话框然后自动继续前进要笨重得多。但我需要建立一些东西并继续前进。我为我的用户感到难过。哦,好吧。

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

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>

【讨论】:

    猜你喜欢
    • 2015-05-31
    • 2022-11-02
    • 1970-01-01
    • 2018-01-08
    • 2013-09-28
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    相关资源
    最近更新 更多