public
Last active

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
jQuery.fn.extend({
// upload file through iframe, without refreshing the page, must be wrapped by form
// jquery.timers dependent
// $("input[type='file']").iframeFileUpload({blank_pathname: '/my_blank_file.html', autoSubmit: true, onStart: function(el){}, onStop(el){}});
iframeFileUpload: function(options) {
return this.each(function(){
var blank_pathname = options.blank_pathname || '/blank.html';
var el = $(this);
var form = el.parents('form');
var iframe_id = 'iframe_' + form.attr('id');
 
if ($('#' + iframe_id).length == 0) {
var iframe = $("<iframe name='" + iframe_id + "'></iframe>"); // holly ie hack
iframe.attr('id', iframe_id);
iframe.css('display', 'none');
iframe.attr('src', blank_pathname);
$('body').prepend(iframe);
}
form.attr('target', iframe_id);
if (options.autoSubmit) {
el.change(function(e){
form.submit();
(options.onStart)(el);
 
jQuery.active++;
jQuery.event.trigger("ajaxStart");
jQuery.event.trigger("ajaxSend");
 
$(document).everyTime(100, iframe_id, function(i) {
if ($('#' + iframe_id).contents()[0].location.pathname != blank_pathname) {
$(document).stopTime(iframe_id);
 
--jQuery.active;
jQuery.event.trigger("ajaxComplete");
jQuery.event.trigger("ajaxStop");
 
iframe.attr('src', blank_pathname);
 
$(document).oneTime(10, function(i) {
(options.onStop)(el);
});
}
});
});
}
});
}
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.