How to upload files asynchronously using jQuery? Last Updated : 03 Aug, 2021 Comments Improve Suggest changes Like Article Like Report To upload files from local machine to the server is called file uploading. It works exactly the same as the definition, when we select file from the browser and click submit button, the browser takes file from local machine and submit it to the server and server does its job to save the file to the defined path. Here use ajax and jQuery to upload a file asynchronously. Used Function: FormData(): It creates a new FormData object. FormData.append(): It appends a new value onto an existing key inside a FormData object, or adds the key if it does not already exist. move_uploaded_file(): It moves an uploaded file to a new location. Steps to run the Program: Create a folder upload in the xampp/htdocs directory. Copy and edit the html/jQuery code as per requirement. Create a file upload.php and copy the php code given below. Start the Apache server and open the html file using browser. Select any text or image file and click on Upload button. The file will be uploaded to the "upload" folder in xamp/htdocs. If the file is an image, it will be displayed as well, if not an image file then "Uploaded file does not have an image" message will be displayed instead. Example: upload.php php <?php /* Getting file name */ $filename = $_FILES['file']['name']; /* Location */ $location = "upload/".$filename; $uploadOk = 1; if($uploadOk == 0){ echo 0; }else{ /* Upload file */ if(move_uploaded_file($_FILES['file']['tmp_name'], $location)){ echo $location; }else{ echo 0; } } ?> HTML File: html <!DOCTYPE html> <html> <head> <title> Async file upload with jQuery </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> </head> <body> <div align="center"> <form method="post" action="" enctype="multipart/form-data" id="myform"> <div > <input type="file" id="file" name="file" /> <input type="button" class="button" value="Upload" id="but_upload"> </div> </form> </div> <script type="text/javascript"> $(document).ready(function() { $("#but_upload").click(function() { var fd = new FormData(); var files = $('#file')[0].files[0]; fd.append('file', files); $.ajax({ url: 'upload.php', type: 'post', data: fd, contentType: false, processData: false, success: function(response){ if(response != 0){ alert('file uploaded'); } else{ alert('file not uploaded'); } }, }); }); }); </script> </body> </html> Output: 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 upload files asynchronously using jQuery? A AnkitMishra16 Follow Improve Article Tags : JavaScript Web Technologies JQuery jQuery-Misc Similar Reads How to upload files using jQuery Dropzone Plugin ? jQuery has many types of file upload plugins that are used to upload various types of files and can be processed further at the backend. Usually, PHP is used widely as backend language with ajax call. We also have dynamic jQuery plugins where we can drag and drop the files. Some of the file uploader 2 min read How to design file upload feature for forms using jQuery EasyUI? EasyUI is an HTML5 framework for using user interface components based on jQuery, React, Angular, and Vue technologies. It helps in building features for interactive web and mobile applications saving a lot of time for developers. Download all the required pre-compiled files from the official websit 2 min read How to load external HTML file using jQuery ? In this article, we will learn how to load an external HTML file into a div element. The following jQuery functions are used in the example codes. ready(): The ready event occurs when the DOM (document object model) has been loaded.load(): The load() method loads data from a server and gets the retu 3 min read How to Upload File with Progress Bar using Node.js ? Uploading files in NodeJS involves transferring files from the client's location to the server's location. A progress bar is a visual component that displays the percentage of the total file upload. In this tutorial, we will create a local server and demonstrate how to upload files to a destination 4 min read How to fire an event on file select using jQuery ? Given an HTML document and the task to fire an event when a file is selected using jQuery. The JQuery change() method is used to fire an event when file is selected. Using change() method: It is used to change the value of the input fields. This method works with "input, textarea and select" element 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 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 Upload File using formidable module in Node.js ? A formidable module is used for parsing form data, especially file uploads. It is easy to use and integrate into your project for handling incoming form data and file uploads.ApproachTo upload file using the formidable module in node we will first install formidable. Then create an HTTP server to ac 2 min read How to upload files using HTML to website ? Every file that needs to be uploaded to the website, required the basic form which facilitates uploading. This feature is essential when we are filling out the form on a specific website. This file upload may support a variety of file formats along with various types of files. The file uploading fea 2 min read How to Use jQueryâs ajax() Function for Asynchronous HTTP Requests ? In this article, we are going to see how we can use jQuery's ajax() function to call backend function asynchronously or in other words HTTP Requests. AJAX is a set of web development techniques used by client-side frameworks and libraries to make asynchronous HTTP calls to the server. AJAX stands fo 4 min read Like