Tengo un plugins de jquery que sube archivos de un formulario usando un iframe. Lo utilizaré como alternativa para quienes no quieran flash.
Actualmente el plugin envía todos los campos de tipo "file" a la vez. Pero me gustaría adaptar el script para que vaya enviando los items uno a uno, y obteniendo una respuesta por cada archivo subido.
Les pido si me orientan porque no se escribir plugins y no entiendo en donde mete los inputs dentro del iframe. Mi idea es hacer un bucle o quizas crear una instancia del plugin por cada input (en vez de aplicar el plugin directo al form). Que ven más factible?
Código Javascript
:
Ver original/**
* jQuery plugin for posting form including file inputs.
*
* Copyright (c) 2010 - 2011 Ewen Elder
*
* Licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* @author: Ewen Elder <ewen at jainaewen dot com> <glomainn at yahoo dot co dot uk>
* @version: 1.1.1 (2011-07-29)
**/
(function ($)
{
$.fn.iframePostForm = function (options)
{
var response,
returnReponse,
element,
status = true,
iframe;
options = $.extend({}, $.fn.iframePostForm.defaults, options);
// Add the iframe.
if (!$('#' + options.iframeID).length)
{
$('body').append('<iframe id="' + options.iframeID + '" name="' + options.iframeID + '" style="display:none" />');
}
return $(this).each(function ()
{
element = $(this);
// Target the iframe.
element.attr('target', options.iframeID);
// Submit listener.
element.submit(function ()
{
// If status is false then abort.
status = options.post.apply(this);
if (status === false)
{
return status;
}
iframe = $('#' + options.iframeID).load(function ()
{
response = iframe.contents().find('body');
console.log(response.html());
if (options.json)
{
try
{
returnReponse = $.parseJSON(response.html());
} catch(err){
returnReponse = response.html();
}
}
else
{
returnReponse = response.html();
}
options.complete.apply(this, [returnReponse]);
iframe.unbind('load');
setTimeout(function ()
{
response.html('');
}, 1);
});
});
});
};
$.fn.iframePostForm.defaults =
{
iframeID : 'iframe-post-form', // Iframe ID.
json : false, // Parse server response as a json object.
post : function () {}, // Form onsubmit.
complete : function (response) {} // After response from the server has been received.
};
})(jQuery);