Foros del Web » Programando para Internet » Jquery »

[SOLUCIONADO] Problema al Subir archivo por $.ajax()

Estas en el tema de Problema al Subir archivo por $.ajax() en el foro de Jquery en Foros del Web. hola amigos. tengo un ejemplo de como subir archivos mediante $.ajax(). el ejemplo funciona. El detalle es que solo funciona con el input type file. ...
  #1 (permalink)  
Antiguo 22/04/2013, 19:01
 
Fecha de Ingreso: abril-2013
Mensajes: 9
Antigüedad: 11 años
Puntos: 0
Pregunta Problema al Subir archivo por $.ajax()

hola amigos. tengo un ejemplo de como subir archivos mediante $.ajax(). el ejemplo funciona.
El detalle es que solo funciona con el input type file.
y necesito tambien agregar un formulario


aqui le dejo el ejemplo. por favor necesito su ayuda.


index.html
Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Documento sin título</title>
  5. <script type="text/javascript" src="js/jquery-1.7.2.min.js" ></script>
  6. <script type="text/javascript">
  7. function uploadAjax(){
  8. var form = $('#form1').serialize();//
  9. /*alert('Datos serializados: '+form);*/
  10. var inputFileImage = document.getElementById("archivoImage");
  11. var file = inputFileImage.files[0];
  12. var data = new FormData();
  13. data.append("archivo",file);
  14. var url = "upload.php";
  15. $.post('variables.php', $("#form1").serialize());
  16.     $.ajax({
  17.         url:url,
  18.         type:"POST",
  19.         dataType: 'json',
  20.         contentType:false,
  21.         data:data,//
  22.         processData:false,
  23.         cache:false
  24.     }).done(function(respuesta){
  25.                     alert(respuesta.mensaje);
  26.                     });
  27.  
  28. }
  29. </head>
  30. <input type="file" name="archivoImage" id="archivoImage" />
  31. <form name="form1" method="post" action="" id="form1">
  32. <label>NOMBRE:</label><input type="text" name="nombre" id="nom"/><br />
  33. <label>APELLIDOS</label><input type="text" name="apellidos" id="ape"/><br />
  34.  
  35. <input type="button" id="botonSubidor" onclick="uploadAjax();"/>
  36. </form>
  37.  
  38. </body>
  39. </html>

upload.php
Código PHP:
Ver original
  1. <?php
  2.  
  3. $upload_folder ="imagenes";
  4.  
  5. $nombre_archivo = $_FILES["archivo"]["name"];
  6.  
  7. $tipo_archivo = $_FILES["archivo"]["type"];
  8.  
  9. $tamano_archivo = $_FILES["archivo"]["size"];
  10.  
  11. $tmp_archivo = $_FILES["archivo"]["tmp_name"];
  12.  
  13. $archivador = $upload_folder . "/" . $nombre_archivo;
  14.  
  15. move_uploaded_file($tmp_archivo, $archivador);
  16. $respuesta = new stdClass();
  17. $respuesta->mensaje='se subio imagen';
  18. echo json_encode($respuesta);
  19. ?>
  #2 (permalink)  
Antiguo 22/04/2013, 20:00
 
Fecha de Ingreso: abril-2013
Mensajes: 9
Antigüedad: 11 años
Puntos: 0
Respuesta: Problema al Subir archivo por $.ajax()

Por fin, di con la solucion.
Para todos los amigos que necesitan un fomulario que permita subir archivos con jquery l la función $.ajax(). aqui la solucion.


index.html
Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Documento sin título</title>
  5. <script type="text/javascript" src="js/jquery-1.7.2.min.js" ></script>
  6. <script type="text/javascript">
  7. function uploadAjax(){
  8. var data = new FormData($('#form1')[0]);
  9. var url = "upload.php";
  10.     $.ajax({
  11.         url:url,
  12.         type:"POST",
  13.         dataType: 'json',
  14.         contentType:false,
  15.         data:data,
  16.         processData:false,
  17.         cache:false
  18.     }).done(function(respuesta){
  19.                     alert(respuesta.mensaje);
  20.                     });
  21.  
  22. }
  23. </head>
  24. <!--<input type="file" name="archivoImage" id="archivoImage" />-->
  25. <form name="form1" method="post" action="" id="form1">
  26. <input type="file" name="archivoImage" id="archivoImage" /><br />
  27. <label>NOMBRE:</label><input type="text" name="nombre" id="nom"/><br />
  28. <label>APELLIDOS</label><input type="text" name="apellidos" id="ape"/><br />
  29.  
  30. <input type="button" id="botonSubidor" onclick="uploadAjax();"/>
  31. </form>
  32.  
  33. </body>
  34. </html>

upload.php
Código PHP:
Ver original
  1. <?php
  2. $upload_folder ="imagenes";
  3.  
  4. $nombre_archivoImage = $_FILES["archivoImage"]["name"];
  5.  
  6.  
  7. $tipo_archivoImage = $_FILES["archivoImage"]["type"];
  8.  
  9. $tamano_archivoImage = $_FILES["archivoImage"]["size"];
  10.  
  11. $tmp_archivoImage = $_FILES["archivoImage"]["tmp_name"];
  12.  
  13. $nom=$_POST['nombre'];
  14. $ape=$_POST['apellidos'];
  15.  
  16. $archivador = $upload_folder . "/" . $nombre_archivoImage;
  17.  
  18. move_uploaded_file($tmp_archivoImage, $archivador);
  19. $respuesta = new stdClass();
  20. $respuesta->mensaje=$nom.' '.$ape.' '.$nombre_archivoImage;
  21. echo json_encode($respuesta);
  22. ?>
  #3 (permalink)  
Antiguo 22/04/2013, 20:27
Avatar de David
Moderador
 
Fecha de Ingreso: abril-2005
Ubicación: In this planet
Mensajes: 15.720
Antigüedad: 19 años
Puntos: 839
Respuesta: Problema al Subir archivo por $.ajax()

Como nota, es importante tener en cuenta que ese código solo funciona en las versiones más recientes de los navegadores (en Internet Explorer 9, por ejemplo, no funciona)
__________________
Por favor, antes de preguntar, revisa la Guía para realizar preguntas.
  #4 (permalink)  
Antiguo 22/04/2013, 20:41
 
Fecha de Ingreso: abril-2013
Mensajes: 9
Antigüedad: 11 años
Puntos: 0
Respuesta: Problema al Subir archivo por $.ajax()

hola david
como podria hacer con las versiones anteriores.
me podrias ayudar a solucionar este problema con respecto a las versiones
  #5 (permalink)  
Antiguo 22/04/2013, 20:56
Avatar de David
Moderador
 
Fecha de Ingreso: abril-2005
Ubicación: In this planet
Mensajes: 15.720
Antigüedad: 19 años
Puntos: 839
Respuesta: Problema al Subir archivo por $.ajax()

Revisa este tema donde tratamos el asunto:
http://www.forosdelweb.com/f179/pasa...e-php-1030451/
__________________
Por favor, antes de preguntar, revisa la Guía para realizar preguntas.

Etiquetas: Ninguno
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 17:15.