Ceicom/html_boilerplate

submitForm

romuloinnocencio opened this issue · 1 comments

  • Implements a formData for file upload as default;
  • I create 'js/components/formData.js' that's validate it ( can be improved );
define([

    'jquery'

],function(){

    if( typeof window.FormData === 'undefined' )
        $('.formee input[type="file"]').attr('hidden',true)
              .parent().append('<div class="msg-box is-warning">Seu navegador é antigo e não permite envio de arquivos. <strong><a href="http://outdatedbrowser.com/pt-br" target="_blank"> Atualizar agora! </a></strong> </div>');        

});

- add some validations inside submitForm file

var formData = new FormData();
var $File = $form.find('input[type="file"]');

 if( $File.length ){

    formData.append( $File.attr('name') , $File[0].files[0] );
    var formInfo = $('form').serialize().split('&');

    for( var i = 0; i < formInfo.length; i++ ){
        var info = formInfo[i].split('=');
        var key = info[0];
        var val = decodeURIComponent(info[1]);

        if( key && val ) 
            formData.append(key, val);
        }
    }       

    $.ajax({
        type: 'POST',
        url: '/modulos/handlers/formularios.ashx?type='+type,
        processData: false,
        contentType: false,
        data: formData,
    });

Done!
Inserted in /js/ux/submitForm.js