# Checking If A File Exists

**URL:** <https://forum.kirupa.com/t/checking-if-a-file-exists/399354>\
**Category:** programming\
**Created:** [December 2, 2014, 2:06am UTC](https://forum.kirupa.com/t/checking-if-a-file-exists/399354 "2014-12-02T02:06:39Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [January 19, 2020, 6:02am UTC](https://forum.kirupa.com/t/checking-if-a-file-exists/399354/11 "2020-01-19T06:02:17Z")

</div>

One thing that all of the solutions posted so far run into is CORS issues when trying to check for content on other domains/hosts than your own. Here is one approach using a CORS bypass proxy that solves that problem:

```
let CORS_override = "https://cors-anywhere.herokuapp.com/";

function doesFileExist(pathToFile) {
  //
  // Remove CORS_override from the 'fetch' call if you'll 
  // only be checking for files on your own domain
  //
  fetch(CORS_override + pathToFile)
    .then(function (response) {
      if (response.status != "404") {
        console.log(pathToFile + " exists: " + response.statusText);
      } else {
        console.log(pathToFile + " does not exist: " + response.statusText);
      }
    })
    .catch(function (error) {
      console.log("Probably a network error!");
    });
}

doesFileExist("https://www.kirupa.com/ssi/newDesign/kirupaLogo_large2.png");
doesFileExist("https://www.kirupa.com/ssi/newDesign/kirupaLogo_large.png");
```

---

_[View the full topic](https://forum.kirupa.com/t/checking-if-a-file-exists/399354)._
