ASP.NET Core 返回文件并从同一操作重定向

Sta*_*rov 1 asp.net asp.net-mvc .net-core asp.net-core

我需要制作这个流程:用户将一些数据发布到操作然后接收文本文件并重定向到另一个操作。

所以我需要返回两个“结果”FileRedirectToAction相同的响应。有没有办法做到这一点?

Nev*_*ane 5

首先建立一个这样的模型:

public class FileAndRedirect {

    public string FileContentAsBase64 { get; set; } 
    public string FileName { get; set; }
    public string RedirectURL { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

然后有一个观点:

@model FileAndRedirect

<a style="display:none" id="linker" href="data:application/octet-stream;charset=utf-8;base64,@Model.FileContentAsBase64" download="@Model.FileName"></a>


<script>

    document.getElementById("linker").click();

    window.location = "@Model.RedirectURL";

</script>
Run Code Online (Sandbox Code Playgroud)

现在您可以返回视图:

return View(new FileAndRedirect{ ... })
Run Code Online (Sandbox Code Playgroud)