Returns both a file and a visualized view in an MVC3 controller action

Is it possible to return the file for loading and update the view from the controller method call?

or a workaround is needed, possibly using javascript (causing the page to refresh after loading)?

file loading is created on the fly according to the parameters set in the ViewModel (search parameters from various controls on the page).

+4
source share
3 answers

You can return the file address using ViewModel or ViewData from your action and use window.location.href , as shown in the following code at the end of your view.

controller

 public ActionResult Index() { /* ... */ ViewBag.FileName = "{FileName}"; return View(); } public ActionResult Download(string id) { /* ... */ return File("{Path}", "{MIME type}", "{Desired file name}"); } 

Markup

 <script type="text/javascript"> window.location.href = "/home/download/" + "@Ajax.JavaScriptStringEncode(@ViewBag.FileName)"; </script> 
+7
source

While it is not possible to return a FileResult with a ViewResult, you can use the meta update on the returned view to redirect the file after the page loads.

 <meta http-equiv="refresh" content="600;url=/path/to/the/file" /> 
+2
source

In one request there can be one answer, what type of response is defined in the response header, so there is no basic answer.
What you can see is to return a view containing javascript, which will then request a file download.

+1
source

Source: https://habr.com/ru/post/1380594/


All Articles