0

I've read several posts on SO before asking my question. Many of them redirected me to the current thing i've right now. Its doing what its supposed, but it seems i'm missing something - because i can't figure out the final part.

My main goal is to submit a file using ajax (for preventing page reload) and then use its contents on another PHP file so i can process its data and display accordingly.

However, i can't figure out how to use the content inside the other php file. No matter what i try i can't find a way to capture the variable result (this variable contains my read file, i can see it trough the developer console).

Basically what i'm trying to achive is: 1)Load a php page with an form to submit an file 2) Prevent refresh after submiting the form 3) Process the submited data on the same page (like display an table with the content)

Thanks in advance!

loadData.php

  <b>ARQUIVO</b><br>
       <form id="formEnvioArquivos">
          <input id="arquivo" name="blobFile" type="file"> 
          <input type="button" id="submitFormData" onclick="enviaDadosForm()" value="Submit" />
    </form>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script type="text/javascript">
    function enviaDadosForm()
    {
        var myFile = $('#arquivo').prop('files')[0];
        var fileReader = new FileReader();
        fileReader.onLoad = function () {
            var final = fileReader.result;
        }
        fileReader.readAsText(myFile);
     
        var fd = new FormData(document.querySelector('#formEnvioArquivos'));
        $.ajax ({
            url: 'submit.php',
            method: 'POST',
            data: {'fileContent' : fileReader.result},
            processData: false,
            contentType: false,
            success: function (data) {
                console.log(myFile);
                console.log(fileReader.result);
            }
        });
    }
    </script>

submit.php

<?php
echo 'FROM PHP: ', $_POST['arquivo'];
echo 'FROM PHP: ', $_FILES['arquivo']['tmp_name'];
?>

Ps: On this post: Submit form without page reloading I almost got what i wanted. However, i couldn't figure out how to pass the contents of the uploaded file instead of form fields.

  • You should retrieve what you're sending in `data: {'fileContent' : fileReader.result}` there : `$_POST['fileContent']` – Cid May 30 '23 at 17:47
  • I've tried print_r($POST['fileContent']); but it prints an empty array. – Pablo Costa May 30 '23 at 17:56
  • the only field which arrive to the PHP is `$_POST['fileContent']` and the value is default for `fileReader.result` not actual file content as you expect, because its not yet loaded at the moment you are trying to send it, `fileReader.onLoad` is asynchronous event. also why are you creating `FormData` object which you don't use after that ? decide which method you wanna use either FileReader or FormData both works for file upload. – Kazz May 30 '23 at 18:46
  • Why are you complicating things like this to begin with, reading the file content yourself ... Add a proper `enctype` to your form, and then just send the FormData instance you created, `data: fd` (You will have to check on `$_FILES['blobFile']` then, because that is your actual file input field name.) – CBroe May 31 '23 at 07:03

0 Answers0