Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116968

Redirect after download

I am new to MVC. I have an index page. Which displays a list of items to the user. The user can then add a new item. After the user has added the new item a file is downloaded for them containing sensitive the information they added. All of that works fine but....

My problem is now how to redirect them back to the index page to refresh the list of items? Its almost like I need a redirect after the download.

Note: Insert download all work fine. I am just trying to figure out how to do a redirect to another model after the download so that I can refresh my list.

I cant do the redirect first because the data that is being saved to the file is sensitive so I don't want to send it anywhere.

[Authorize]
public async Task<IActionResult> Index(int clientId, string error)
    {
     // ....... Removed
     var model = new SecretIndexModel()
        {
            Client = clients.Client,
            ErrorMessage = error
        };
        return View(model);
    }

On this page, a user can add a new item.

[Authorize]
[HttpPost]
public async Task<IActionResult> Add(SecretIndexModel model)
{

 /// removed ....

 return DownloadDocument("results json string"); 
 }

Download

 public FileResult DownloadDocument(string id)
    {
        var ms = new MemoryStream(System.Text.Encoding.ASCII.GetBytes("abc"));

        return File(ms, System.Net.Mime.MediaTypeNames.Application.Octet, "secret.json");
    }

Am I going about this wrong? I know I need to add something like this because that works if I return that instead of the download.

return RedirectToAction("Index",
       new
          {
           clientId = model.Client.Id,
           error = (client.Errors == null)
                   ? string.Empty
                   : client?.Errors?.FirstOrDefault()?.Message
          });

Update:

I can send the data over using TempData but that doesn't seem to help much I can then refresh the index page but not download the file.

Upvotes: 1

Views: 891

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239290

As @ADyson stated in the comments, a download and a redirect are two different response, and you can only return one response. Personally, I would return the redirect and then simply include a link to download the file. You could save the link URL to TempData and present it like an alert or toast notification. You can also set a meta refresh tag to automatically request the download. For and example, check the Visual Studio download page. It loads a page with a link to download the file in case it doesn't start automatically, but then after a second or two, you get an automatic prompt to download.

If you really want to take it farther, and especially if the download takes some amount of time to create, you could start the download file creation process and immediately return the redirect. Then, using SignalR, you can notify the user when the download is actually ready or even automatically download it at that point. This provides a much more ideal user experience, again, if the download takes some period of time to create. Instead of making the user way staring at a blank page, they get a response near immediately and get their download when it's ready.

Upvotes: 1

Related Questions