Ajax to download a file


I would like to download a file in an asynchronous manner. From what I
have read, I should be using a hidden iframe. How do I connect the AJAX
request to the iframe to display the file?



  • Of you use AJAX to download the file, you don't need an Iframe. If you do use an IFrame, you don't need AJAX. It's one of the two... I would prefer AJAX, simply because it gives you more freedom... Except it you want to display a full page including head and body tags and included scripts from a third party, then I would suggest using an IFrame so you can really seperate it from your own HTML and the scripts won't conflict etc.

    If you're a beginner with AJAX, I could really recommend to use some JavaScript Framework with build in AJAX support like mootools, scriptaculous or jquery. I never used jquery, but I did use mootools and scriptaculous and I would say that mootools is easier to master because documentation is very good on their site. scriptaculous is also very powerfull and some say even better then mootools (http://mootools.net/), but if you're not allready a guru, you need to put in a lot of time to google for examples on everything..., So I would say check out mootools and try something like this:


    function GetMyAJAXFile() {
    var myHTMLRequest = new Request.HTML({
    onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
    // This will b e triggered after the document is loaded succesfully
    $('MyDiv').innerHTML = responseHTML;


    The dollar function is short for document.getELementById() very handy and you'll use that one quite often. With this example you'll get the data and place it inside a div element. No Iframe needed. Make sure the document you load is just HTML without around it, because it will be rendered inside the current HTML file.

    Hope this helps ya out. (I didn;t test the example above, but it will steer you in a direction I hope. Check out the documentation on the mootools.net site for more cool stuff you can do with it.)
    the Netherlands...
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion