How to Check Mentioned File Exists or not using JavaScript/jQuery? Last Updated : 07 Apr, 2025 Summarize Comments Improve Suggest changes Share 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 P priyankahiranandani Follow Improve Article Tags : JavaScript CSS-Misc HTML-Misc jQuery-Misc JavaScript-Misc HTML-Questions CSS-Questions JavaScript-Questions jQuery-Questions +5 More Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac 13 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi 6 min read Like