...

View Full Version : Problem with file download script



macleodjb
10-02-2011, 09:31 PM
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.


$('#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.



(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.

var body = (iframe.prop('contentDocument') !== undefined)


Any suggestions?
Thanks in advance.

macleodjb
10-03-2011, 03:29 AM
turns out the .prop property was only available in the latest version of jquery. Once I switched everything worked fine.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum