Home > Software engineering >  Check a file before submit the form (Symfony, Ajax)
Check a file before submit the form (Symfony, Ajax)

Time:11-24

I need to check column of an excel file before submitting a form, on a onChange event on my symfony input. I wanna use an Ajax request to send my file to a php function which will check CSV or excel columns. But when i get my file i have a fakepath navigator security... That's my code :

{{ form_widget(form.import, {'attr' : {'onchange' : 'test()'}}) }}
function test(){
    const input = document.getElementById('form_import');
    $.ajax({
        type: 'POST',
        url: '/admin/check_import',
        data : {input.file},
        success:  function(){
            console.log("Ok");
        }
    })
}
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
{% endblock %}

CodePudding user response:

use this one:

var input = new FormData(docuemnt.getElementById('form_import'));

 $.ajax({
        type: 'POST',
        url: '/admin/check_import',
        enctype: 'multipart/form-data',
        processData: false,
        contentType: false,
        cache: false,
        timeout: 600000,
        data : input,
        success:  function(){
            console.log("Ok");
        }
    });

and in your php backend just refer to file with $_FILES[' the "name" attribute of the input']

CodePudding user response:

I can't implement a form Data. I have this error. "FormData constructor: Argument 1 does not implement interface HTMLFormElement."

  • Related