Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts

    Problem with file download script

    Hi Guys,

    I am trying to create a way for my users to download some code dynamically from my web page via a file download. Below is the code that i have written so far. It seems to be dying on the iframe but i'm not sure why.

    Here is my jquery trigger which is inside my onreadystate function.
    Code:
    	$('#export_txt').click(function(e){
    		alert(LPAjax.ajaxurl + "/download.php");
    		$.generateFile({
    			action		: "download_txt",
    			filename	: "exportme.txt",
    			content		: $("#ticket_window").html(),
    			script		: LPAjax.ajaxurl + "/download.php"
    		});
    		e.preventDefault();
    	});
    and here is the code i am using to process the post.

    Code:
    (function($){
    	
    	// Creating a jQuery plugin:
    	
    	$.generateFile = function(options){
    		
    		options = options || {};
    		
    		if(!options.script || !options.filename || !options.content){
    			throw new Error("Please enter all the required config options!");
    		}
    		alert("Inside Function");
    		// Creating a 1 by 1 px invisible iframe:
    		
    		var iframe = $('<iframe>',{
    			width:1,
    			height:1,
    			frameborder:0,
    			css:{
    				display:'none'
    			}
    		}).appendTo('body');
    		
    		var formHTML = '<form action="" method="post">'+
    			'<input type="hidden" name="filename" />'+
    			'<input type="hidden" name="content" />'+
    			'</form>';
    		
    		// Giving IE a chance to build the DOM in
    		// the iframe with a short timeout:
    		
    		setTimeout(function(){
    		
    			// The body element of the iframe document:
    		
    			var body = (iframe.prop('contentDocument') !== undefined) ?
    							iframe.prop('contentDocument').body :
    							iframe.prop('document').body;	// IE
    			alert(" We are almost there");
    			body = $(body);
    			
    			// Adding the form to the body:
    			body.html(formHTML);
    			alert("added form to page");
    			var form = body.find('form');
    			
    			form.attr('action',options.script);
    			form.find('input[name=filename]').val(options.filename);
    			form.find('input[name=content]').val(options.content);
    			alert("submitting form");
    			// Submitting the form to download.php. This will
    			// cause the file download dialog box to appear.
    			
    			form.submit();
    		},50);
    	};
    	
    })(jQuery);
    it seems to be dying on this line.
    Code:
    var body = (iframe.prop('contentDocument') !== undefined)
    Any suggestions?
    Thanks in advance.

  • #2
    Regular Coder
    Join Date
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts
    turns out the .prop property was only available in the latest version of jquery. Once I switched everything worked fine.


  •  

    Posting Permissions

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