79168119

Date: 2024-11-07 21:28:48
Score: 2
Natty:
Report link

So based on what @browsermator suggested, a much simpler solution was to change the button to a "submit" type button, add asp-controller and asp-action to the button, and have the action just return the file. So much easier. Thank you @browsemator!

View - Just the button here...

<button type="submit" asp-controller="MyPageController" asp-action="DownloadFile"> 
    Download 
</button>

Controller

[HttpPost]
[Route("MyPageController/DownloadFile")]
public ActionResult DownloadFile(string fileName)
{
    // This is an example for excel.
    string fileType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";


    MemoryStream stream = new MemoryStream();
    
    ... Load your memory stream...

    //for me and how I had to load the memory stream, 
    //i needed to reset the stream to position 0.
    stream.Position = 0;

    //return the File object.  I had no idea the post wouldn't reload the page.
    return File(stream, fileType, fileName);
}
Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Blacklisted phrase (0.5): i need
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @browsermator
  • User mentioned (0): @browsemator
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Rob K.