Link to home
Start Free TrialLog in
Avatar of Donnie Walker
Donnie WalkerFlag for United States of America

asked on

Is there a way to check if a file exists with javascript or jquery?

I've got a form that allows a user to upload a file.

Is there a way to check if that file exists after uploading using javascript or jquery?
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Donnie Walker

ASKER

Can you provide an example?
when I run this I get nothing:

<script>
$.ajax({
    type: 'HEAD',
    url: 'http://www.example.com/index.php',
    success: function() {
        alert('Page found.');
    },  
    error: function() {
        alert('Page not found.');
    }
});
</script>
work for me :

found : http://jsfiddle.net/cyVGM/4/
not found : http://jsfiddle.net/cyVGM/

be sure to have jQuery in place
I can't get that to work on any file that is on another server.  It will work for local files but not remote ones.
This is what I have and I get nothing:

<html>
<head>

      <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
      <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.22/jquery-ui.min.js"></script>
</head>

<body>

<script>
function fileExists(fileLocation) {
    var response = $.ajax({
        url: fileLocation,
        type: 'HEAD',
        async: false
    }).status;
    alert(response);
}
</script>

<script> fileExists('/Uploads/test.pdf'); </script>

</body>
</html>
maybe that is why mine is not working. Two different servers on the same network.