Skip to content
the poor coder

javascript check if file exists on server

Poor Man
Published
Reading time
2 min read
On this page

How to Check if a File Exists on the Server using Javascript?#

If you are working on a web application that requires downloading files from the server, it is necessary to check whether the file exists on the server before attempting to download it. In this case, Javascript comes in handy to perform this task.

Method 1: Using XMLHttpRequest Object#

The XMLHttpRequest object is used to make AJAX requests to the server. We can use this object to check whether a file exists on the server or not. The following code snippet demonstrates how to check if a file exists on the server using the XMLHttpRequest object:

Text

function fileExists(url) {
  var http = new XMLHttpRequest();
  http.open('HEAD', url, false);
  http.send();
  return http.status != 404;
}

In this code snippet, we have defined a function named fileExists() that takes the URL of the file as an argument. The function creates an instance of the XMLHttpRequest object and sends a head request to the server using the http.open() method. The http.send() method sends the request to the server. If the file exists on the server, the status code returned by the server will be 200. If the file does not exist, the server will return a 404 status code. We check whether the status code is not equal to 404 to determine whether the file exists or not.

Method 2: Using jQuery#

If you are using jQuery in your web application, you can use its $.ajax() method to check whether a file exists on the server. The following code snippet demonstrates how to use jQuery to check if a file exists on the server:

Text

function fileExists(url) {
  var http = $.ajax({
    type:"HEAD",
    url: url,
    async: false
  });
  return http.status != 404;
}

Here, we have defined a function named fileExists() that takes the URL of the file as an argument. The function uses the $.ajax() method to send a head request to the server using the type:"HEAD" option. The async:false option makes the request synchronous. The status code returned by the server is checked to determine whether the file exists or not.

Conclusion#

These were the two methods to check whether a file exists on the server using Javascript. You can use any of these methods depending on your requirements. However, it is recommended to use the XMLHttpRequest method as it is a native Javascript method and does not require any external library.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC