I have an html interface to my server. On the interface is a button. When a user clicks the button, they download a file located on the server.
My current implementation appends an iframe to the html document, after the user presses the button. The iframe has it’s url parameter set to the uri of the file on the server, and a display parameter of “none”. It is appended to the body of the document.
My question is, what is the best practice for achieving a result like this? i.e. How should the button handler manage the download?
I would say that Best Practice is to use a form with your buttons. E.g:
If you really need it to be targeted at an IFRAME (which seems unnecessary as aroth explained – and you can always set the Content-Disposition response header for recognised types), you can put
target="myIFrame"in your form tag (myIFrame being the name of your hidden IFRAME).In fact, the Content-Disposition response header is the Best Practice for ensuring the file is downloaded rather than opened.