jQuery Ajax to download a file-VBForums
Results 1 to 2 of 2

Thread: jQuery Ajax to download a file

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    15,870

    jQuery Ajax to download a file

    Has anyone downloaded files with a jQuery ajax call - to a web service would be ideal...

    We fooled with MS Ajax Downloader stuff months ago - didn't like it and can't recall why.

    Also did jDownload testing months ago - but that seems to be defunct now.

    Anyone doing anything like this?

    I want to download PDF files using ajax.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    15,870

    Re: jQuery Ajax to download a file

    I found this code - and don't understand it much - but it's calling my download.aspx page

    Code:
    $.download = function(url, data, method, callback){ 
        var inputs = ''; 
        var iframeX; 
        var downloadInterval; 
        if(url && data){ 
            // remove old iframe if has 
            if($("#iframeX")) $("#iframeX").remove(); 
            // creater new iframe 
            iframeX= $('<iframe src="[removed]false;" name="iframeX" id="iframeX"></iframe>').appendTo('body').hide(); 
            if($.browser.msie){ 
                downloadInterval = setInterval(function(){ 
                    // if loading then readyState is “loading” else readyState is “interactive” 
                    if(iframeX&& iframeX[0].readyState !=="loading"){ 
                        callback(); 
                        clearInterval(downloadInterval);
                    }
                }, 23); 
            } else { 
                iframeX.load(function(){ 
                    callback(); 
                });
            }
            //split params into form inputs 
            $.each(data, function(p, val){ 
                inputs+='<input type="hidden" name="'+ p +'" value="'+ val +'" />';
            });
            //create form to send request 
            $('<form action="'+ url +'" method="'+ (method||'post') + '" target="iframeX">'+inputs+'</form>').appendTo('body').submit().remove();
        };
    };
    I tried calling it like this - but the filename isn't passing in.

    Code:
    $.download('Download.aspx', 'filename=mitlicense.txt');
    Can someone help explain to me the iframe and the input type HIDDEN stuff it's doing?

    Thanks!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.