Ver Mensaje Individual
  #8 (permalink)  
Antiguo 13/04/2017, 12:23
alvaro_trewhela
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Visualizar PDF usando ajax y php

Por parte del servidor obtener la ruta del archivo y muestras..

Código HTML:
Ver original
  1.     <head>
  2.         <script type="text/javascript">
  3.         function seePdf(){
  4.         //todo tu ajax aca...
  5.         document.getElementById("pdf").setAttribute("src", respuestaAjax);
  6.         }
  7.         </script>
  8.     </head>
  9.     <body>
  10.         <embed src="RESPUESTA RUTA ACA" id="pdf" width="ancho" height="alto" />
  11.     </body>
  12. </html>

Por php:

Código PHP:
Ver original
  1. <?php
  2.  
  3. $ruta = "tu/ruta"; // ruta completa sin / final
  4.  
  5. $pdf = $_POST["recurso"];
  6. //o get no se cual usas...
  7. $pdf = $_GET["recurso"];
  8.  
  9. /*
  10.  
  11. aca toda progra...
  12.  
  13. */
  14.  
  15. echo "$ruta/$retornoDeTuProgra";
  16.  
  17. ?>

Yo probé esto:

Código HTML:
Ver original
  1.     <head>
  2.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  3.         <title>AJAX</title>
  4.        
  5.         <script type="text/javascript">
  6.        
  7.        
  8.         function createREQ() {
  9.                 try {
  10.                 req = new XMLHttpRequest(); /* p.e. Firefox */
  11.                 }
  12.                 catch(err1){
  13.                     try {
  14.                     req = new ActiveXObject('Msxml2.XMLHTTP'); /* algunas versiones IE */
  15.                     }
  16.                     catch (err2) {
  17.                         try {
  18.                         req = new ActiveXObject("Microsoft.XMLHTTP"); /* algunas versiones IE */
  19.                         }
  20.                         catch (err3) {
  21.                         req = false;
  22.                         }
  23.                     }
  24.                 }
  25.         return req;
  26.         }
  27.        
  28.        
  29.         function mostrarpdf(){
  30.         var pdf = document.getElementById("pdf");
  31.             if(pdf.value == "0"){ alert("elija!!"); return false; }
  32.         var http = new createREQ();
  33.         http.open("POST", "seePdf.php", true);
  34.         http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  35.         var archivo = document.getElementById("pdf").value;
  36.         http.send("archivo="+archivo); 
  37.        
  38.         http.onreadystatechange = function(){
  39.                 if(http.readyState == 4 && http.status == 200){
  40.                 var response = http.responseText;
  41.                 document.getElementById("emb").setAttribute("src", response);
  42.                 }
  43.             };
  44.         }
  45.         </script>
  46.    
  47.     </head>
  48.     <body>
  49.         <select id="pdf" onchange="mostrarpdf();">
  50.             <option value="0">Elejir</option>
  51.             <option value="cotizacion.pdf">Pdf cotizacion</option>
  52.             <option value="tarea.pdf">Pdf tarea</option>
  53.             <option value="certificado.pdf">Pdf certificado</option>
  54.         </select>
  55.         <br/><br/><br/>
  56.         <embed src="" width="500" height="500" id="emb"/>
  57.     </body>
  58. </html>

PHP:

Código PHP:
Ver original
  1. <?php
  2.  
  3. $ruta = "archivos/pdf"; //ruta sin / final
  4.  
  5. $archivo = $_POST["archivo"];
  6.  
  7. echo "$ruta/$archivo";
  8.  
  9. ?>

y tengo un escritorio con los pdf...

usa eso como ejemplo, no se si es lo mejor, este es mi template de ajax

Última edición por alvaro_trewhela; 13/04/2017 a las 13:10