# 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:** 7

<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:** [May 19, 2017, 11:08am UTC](https://forum.kirupa.com/t/checking-if-a-file-exists/399354/7 "2017-05-19T11:08:33Z")

</div>

Since @senocular picks on me for using xhr instead of Fetch, I figured I’d post a solution that uses the newer Fetch API instead 😛

```
fetch("https://www.kirupa.com/book/images/learning_react2.gif", {
    method: "head",
    mode: "no-cors"
})
.then(function(response) {
    if (response.status == 200) {
        console.log("file exists!");
    } else {
        console.log("file doesn't exist!");
    }
    return response.text();
})
.catch(function(error) {
  console.log("Error ", error);
});

```

The result should be nearly identical!

Cheers,  
Kirupa

---

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