When making a recent update to a web application, I came across a need to change the method of downloading a file from a simple click on a link (HTML <a> tag) to submitting data to the server via a POST request. The reason for needing to do this is based on the code on the server side that I was hooking into – so there really was little option when it came to how the file was fetched – it had to be via POST. The file also does not exist on the server – it is dynamically created when the link is clicked on, and I did not want to have to create a file temporarily.
When the form is submitted, it will return to the iFrame. The iFrame will see from the type of data being returned that it is a file, and the browser will download the file. This is a very basic solution, to which there can be multiple extensions. The first, and probably most obvious is what to do if there is an error fetching the file – the code above does not deal with that scenario – it always assumes that a file will be generated and sent for download.