ASP.NET MVC How to output FileContent from within a controller action and continue

1.4k Views Asked by At

I am fairly new to MVC and want to know what the correct approach is - when a form is submitted the Edit ActionResult is executed, once the data is saved, I want to generate a file and send to the browser for download but also continue to another action.

The file is sent to the browser but no further processing occurs in the controller, because I use return RedirectToActions. Thanks in advance for any pointers.

public ActionResult Edit(int id, InvoiceFormData formData)  
{ 
  ...    
  return base.RedirectToAction("ReturnPdf", new { id = 99 });

  // More processing...ideally...then

  return base.RedirectToAction("Action", "Controller");
}

public FileResult ReturnPdf(int? id)
{
  ...
  return output; // writes file output and prompts user to download
}
1

There are 1 best solutions below

1
On

You cannot use two return statements in the same action result and expect them to both run. When you "return" it means you're returning the output and then quiting out of the action result. Therefore, you could kind of chain your actions, so rather than doing the further processing inside the Edit action result, you could move that logic into the ReturnPdf AR(since that's what's I could see in your code), then finally return the final output in whatever action result you landed on. I hope that was clear enough...