So I came up with a slightly hacky and inefficient solution. The basic idea is to post the data to the server and get the server to return it to the client as a download. The HTML looks like this
<form action="download.php" method="post"> <div> <input type="hidden" name="fileName" value="locations.csv" /> <input type="submit" value="Download" /> </div> <textarea id="geocodedPostcodes" style="width:100%;" rows="20" name="data"></textarea> </form>
All that is needed is a hidden field that tells the server-side script what the download file name should be and a text area with a name of “data”.
The server-side script is pretty simple, it looks like this
<?php header('Content-Disposition: attachment; filename="' . $_POST["fileName"] . '"'); // add data print($_POST["data"]); ?>
All it does is get the requested file name and echo back the data.
It’s seems a bit crazy (and a waste of bandwidth) that this seems to be the only way to achieve a seemingly simple task, but that looks to be the case. I’d be happy to be proved wrong.