Page 1 of 1

Verificar si existe el archivo y lanzar

PostPosted: Tue Jul 13, 2021 2:13 am
by Patricio Avalos Aguirre
Estimados

como puedo saber si existe el archivo antes de lanzar el PDF

he tratado de buscar en la web pero no lo he podido lograr

Code: Select all  Expand view
BUTTON ID 'btnprint'  LABEL ' Print'  ACTION 'PrintPdf()' GRID 2 ICON '<i class="fas fa-home"></i>' CLASS 'btn-primary btnticket' OF o


Code: Select all  Expand view
<script>
               
                function PrintPdf() {
                  if existarchivo( 'cFileName.Pdf' )
                    window.open('cFileName.Pdf' );
                 }
 

Re: Verificar si existe el archivo y lanzar

PostPosted: Tue Jul 13, 2021 11:26 pm
by Otto
Hello,
Please search for: javascript check file exists on server
I found: How do I check if file exists in jQuery or pure JavaScript?
https://stackoverflow.com/questions/364 ... javascript
Best regards,
Otto

Re: Verificar si existe el archivo y lanzar

PostPosted: Tue Jul 13, 2021 11:53 pm
by Otto
Code: Select all  Expand view


function main

TEMPLATE
<!DOCTYPE html>
<html>
<head>
</head>
<body>
   
    <h1>Test open PDF</h1>
    <button onclick="myFunction()">Open PDF</button>
   
    <script>
        function myFunction() {
            var url = "https://winhotel.space/modharbour_samples/test.pdf";

            if ( fileExists( url ) ){
            var myWindow = window.open("https://winhotel.space/modharbour_samples/test.pdf", "", "width=400,height=400");
            }else{
            alert("file not found");                
            }
               
        }
       
       
        function fileExists(url) {
            if(url){
                var req = new XMLHttpRequest();
                req.open('GET', url, false);
                req.send();
                return req.status==200;
            } else {
                return false;
            }
        }
       
    </script>
   
</body>
</html>

ENDTEXT

return
 

RESUELTO Re: Verificar si existe el archivo y lanzar

PostPosted: Wed Jul 14, 2021 2:57 am
by Patricio Avalos Aguirre
Gracias Otto

FUNCIONO!!!