Redirect / show view after generated file is dowloaded

前端 未结 3 2120
予麋鹿
予麋鹿 2020-11-28 13:57

I\'ve got a controller action that downloads a dynamically generated file:

    public ActionResult DownloadFile()
    {
        var obj = new MyClass { MyStr         


        
3条回答
  •  有刺的猬
    2020-11-28 14:30

    As Ross has said, you can only return one response to a HTTP request. What i do in that case is:

    1. Send the request to the server
    2. The server generates the file and stores it in some server side data structure (Cache, Usersession, TempData)
    3. The server returns a RedirectToAction() (POST, REDIRECT, GET pattern)
    4. The redirected action returns a View with some javascript which
    5. Triggers the download of the pregenerated file by setting window.location.href property to an special download action which sends the file back to the browser

提交回复
热议问题