Ver Mensaje Individual
  #1 (permalink)  
Antiguo 18/04/2016, 05:41
Avatar de cluster28
cluster28
 
Fecha de Ingreso: enero-2008
Ubicación: Donostia - San Sebastián
Mensajes: 756
Antigüedad: 17 años, 1 mes
Puntos: 32
Recuperar fichero enviado por AJAX

Buenas,

No se si es el problema es de Jquery o de PHP. Tengo este fichero de prueba y soy incapaz de recuperar el fichero enviado a través de Ajax. Si lo mando normal no hay problema. A través de Ajax los arrays $_POST y $_FILES están siempre vacíos.

Código PHP:
Ver original
  1. <!doctype html>
  2. <html>
  3.     <head>
  4.     <meta charset="utf-8">
  5.     <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  6. </head>
  7.     <body>
  8.        
  9.         <?php
  10.         echo '<pre>';
  11.         print_r($_POST);
  12.         print_r($_GET);
  13.         print_r($_FILES);
  14.         print_r($_SERVER);
  15.         echo '</pre>';
  16.         ?>
  17.         <div id="add-file">
  18.             <form id="form-file" action="index.php" method="post">
  19.                 <input id="fileupload" type="file" name="fichero"/>
  20.                 <input type="hidden" name="subcarpeta" value=""/>
  21.                 <input type="hidden" name="id_usuario" value=""/>
  22.                 <input type="submit"></input>
  23.             </form>
  24.         </div>
  25.         <script>
  26.  
  27.               $('#form-file').submit(function(event){
  28.  
  29.                     event.preventDefault();
  30.  
  31.                     $.ajax({
  32.  
  33.                             error:      function(jqXHR, textStatus, errorThrown){},
  34.                             data:       {
  35.                                             datos: new FormData($(this)[0])
  36.                                         },
  37.                             success:    function(data, textStatus, jqXHR)
  38.                                         {
  39.                                             console.log(data);
  40.                                         },
  41.                             url:        "index.php",
  42.                             method:     "POST",
  43.                             processData: false,
  44.                             contentType: false,
  45.                             cache: false,
  46.                             dataType: "html"
  47.                         }
  48.                     );
  49.                 });
  50.  
  51.         </script>
  52.     </body>
  53. </html>