Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] Recuperar fichero enviado por AJAX

Estas en el tema de Recuperar fichero enviado por AJAX en el foro de PHP en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 18/04/2016, 05:41
Avatar de cluster28  
Fecha de Ingreso: enero-2008
Ubicación: Donostia - San Sebastián
Mensajes: 756
Antigüedad: 16 años, 4 meses
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>
  #2 (permalink)  
Antiguo 18/04/2016, 05:56
 
Fecha de Ingreso: octubre-2010
Ubicación: España
Mensajes: 1.007
Antigüedad: 13 años, 6 meses
Puntos: 123
Respuesta: Recuperar fichero enviado por AJAX

Creo que algo no está bien en el js echale un vistazo a esto
__________________
Unset($vida['malRollo']);
  #3 (permalink)  
Antiguo 18/04/2016, 07:21
Avatar de cluster28  
Fecha de Ingreso: enero-2008
Ubicación: Donostia - San Sebastián
Mensajes: 756
Antigüedad: 16 años, 4 meses
Puntos: 32
Respuesta: Recuperar fichero enviado por AJAX

Efectivamente, había una cosa mal en el javascript. El parámetro data.

Código Javascript:
Ver original
  1. $('#form-file').submit(function(event){
  2.  
  3.     event.preventDefault();
  4.  
  5.     $.ajax({
  6.  
  7.         error:      function(jqXHR, textStatus, errorThrown){},
  8.         data:       new FormData($(this)[0]),
  9.         success:    function(data, textStatus, jqXHR)
  10.                         {
  11.                               console.log(data);
  12.                         },
  13.          url:        "index.php",
  14.          method:     "POST",
  15.          processData: false,
  16.          contentType: false,
  17.          cache: false,
  18.          dataType: "html"
  19.      });
  20. });

Etiquetas: ajax, enviado, fichero, html
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 15:45.