Send a file with ajax and process it with php

0

I am developing a text editor, I am trying to upload live files, where you load the file and it appears in the text editor similar to word, I am doing it without a form so it is more complicated to process it with PHP, I want that when loading the file this immediately load it in the server, open it and execute it in the client with AJAX

        //INPUT FILE
        $("input[name=loadtxt]").change(function(event){
           $.ajax({
              url:'http://localhost/proyecto_servidor/loadtxt.php?file='+this.files[0],
              method:'POST',
              success:function(data){
                 //response here
              },
              error:function(data){
                  console.log(data);
              }
           });
        });
<input type='file' name='loadtxt' accept='.txt' class='firepad-btn firepad-dropdown'>

Try sending it by the URL, and it arrives in PHP as (Object File) but you can not access these variables offered by the file such as name, size etc, the idea is that in php, open the file with that name and I loaded it with fopen

    
asked by Carlos Estarita 14.11.2017 в 19:34
source

1 answer

1

I do not know if an image can be sent by url or not, the way I have always sent images to the server is as follows:

In the JS

$("input[name=loadtxt]").change(function(event){
    var file = this.files[0];

    var datos = new FormData();
    datos.append('file', file);

    $.ajax({
        url:'http://localhost/proyecto_servidor/loadtxt.php',
        data: datos,
        method:'POST',
        success:function(data){
            //response here
        },
        error:function(data){
            console.log(data);
        }
    });
});

In PHP

$nombre_archivo = $_FILES['file']['name'];
$ruta_temporal_archivo = $_FILES['file']['tmp_name'];

I leave this Link where you can find more information about $_FILES

    
answered by 14.11.2017 в 19:56