How to Check Mentioned File Exists or not using JavaScript/jQuery? Last Updated : 07 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Sometimes we want to upload a file through the path of the file, but a few times the file does not exist. In that case, we have to respond to users the given path does not find any file or does not exist in the mentioned file. Below are the approaches to check if mentioned file exists or not using JavaScript/jQuery:Table of Content Using Ajax() method Using XMLHttpRequest() methodUsing Ajax() method Ajax method () of jQuery to check if a file exists on a given URL or not. The Ajax () method is used to trigger the asynchronous HTTP request. If the file exists, the ajax() method will in turn call the ajaxSuccess() method or else it will call the error functionExample: To demonstrate checking whether the mentioned file exists or not using the Ajax() method. HTML <!DOCTYPE html> <html> <head> <title> How to check if file exist using jquery </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> <style> body { text-align: center; } h1 { color: green; } #output { color: green; font-size: 20px; font-weight: bold; } </style> </head> <body> <h1> GeeksforGeeks </h1> <label id="File_Path"> <b>Enter File Path:</b> </label> <input type="text" id="File_URL"> <button id="Check_File"> click here </button> <p id="output"></p> <script> $(document).ready(function () { $("#Check_File").click(function () { var url = $("#File_URL").val(); if (url != "") { $.ajax({ url: url, type: 'HEAD', error: function () { $("#output").text("File doesn't exists"); }, success: function () { $("#output").text('File exists'); } }); } else { $("#Output").text("Please enter File URL"); } }); }); </script> </body> </html> Output:Using XMLHttpRequest() methodTo trigger ajax request. If the HTTP status is 200 then file exists otherwise file is not present. Example: To demonstrate using the xmlhttp request() method for checking wether the mentioned files exit or not. HTML <!DOCTYPE HTML> <html> <head> <title> How to check if file exist or not on HTTP status is 200 </title> <style> body { text-align: center; } h1 { color: green; } #output { color: green; font-size: 20px; font-weight: bold; } </style> </head> <body> <h1> GeeksforGeeks </h1> <label id="File_Path"> <b>Enter File Path: </b> </label> <input type="text" id="File_URL"> <button id="Check_File" onclick="checkFileExist()"> click here </button> <p id="output"></p> <script> var url = document.getElementById("File_URL"); var output = document.getElementById("output"); var http = new XMLHttpRequest(); function checkFileExist() { if (url.length === 0) { output.innerHTML = "Please enter File URL"; } else { http.open('HEAD', url, false); http.send(); if (http.status === 200) { output.innerHTML = "File exists"; } else { output.innerHTML = "File doesn't exists"; } } } </script> </body> </html> Output: HTTP status is not 200 so if the file exist it will show not exist until the status is 200. jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it's philosophy of “Write less, do more". You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples. Comment More infoAdvertise with us Next Article How to Check Mentioned File Exists or not using JavaScript/jQuery? P priyankahiranandani Follow Improve Article Tags : JavaScript Web Technologies JQuery CSS-Misc HTML-Misc jQuery-Misc JavaScript-Misc HTML-Questions CSS-Questions JavaScript-Questions jQuery-Questions +7 More Similar Reads How to check input file is empty or not using JavaScript/jQuery ? Given an HTML document containing an input element, the task is to check whether an input element is empty or not with the help of JavaScript. These are the two approaches to check input file is empty or not using JavaScript/jQuery: Table of Content Using element.files.length property in JavaScript 2 min read How to download File Using JavaScript/jQuery ? The ability to download files directly from a website is an essential feature for many web applications. Whether you're providing documents, images, or other data, enabling users to download files seamlessly enhances their experience and ensures they can access the content offline. This article prov 2 min read How to create a File Input using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a File Input Area using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ hr 1 min read How to check a URL contains a hash or not using JavaScript ? In this article, the task is to check whether an URL contains or not. This can be done by using the Location hash property in JavaScript. It returns the string which represents the anchor part of a URL including the hash â#â sign. Syntax: window.location.hash Example: This example uses the hash prop 1 min read How to check element exists or not in jQuery ? Checking if an element exists in jQuery involves selecting the element and verifying its length. If the selected element's length is greater than 0, the element exists in the DOM; otherwise, it doesn't. This is useful for conditionally manipulating elements. There are two ways to check whether an el 3 min read How to check an array is empty or not using jQuery ? In this article, we will check if an array is empty or not using jQuery. In JavaScript, arrays are a special type of object. If we use the typeof operator for arrays, it returns "object". We can use jQuery's isEmptyObject() method to check whether the array is empty or contains elements. The isEmpty 2 min read Bash Scripting - How to check If File Exists In this article, we will write a bash script to check if files exist or not. Syntax :test [expression][ expression ][[ expression ]] Here, in expression, we write parameter and file name. Let us see some parameters that can be used in the expression: - -f: It returns True if the file exists as a com 6 min read How to check whether an object exists in javascript ? Checking whether an object exists in JavaScript refers to determining if a variable or property has been declared and contains a valid value. This helps avoid errors when accessing or manipulating objects that may be undefined, null, or not initialized properly.Here we have some common approaches to 3 min read How to check if File Exists in PHP ? To check whether any file is existing or not then we can use the below-mentioned PHP function. To find the existence of the files, we use file_exists() function. This function is used to check whether a file or directory exists or not. Syntax: file_exists( $path ) Parameters: This function accept on 1 min read How to check if a file exists in Ruby? This article will discuss how to check if a file exists in Ruby. Checking if a file exists is important whether you are managing file operations, creating applications or handling data. The intention of this guide however is to offer a detailed account of methods that can be used to effectively achi 2 min read Like