Javascript-Jquery How to use ajax to upload file from browser

Sovary October 1, 2021 418
33 seconds read

Uploading any files from client to server is quite important. This article shows the ways of implementing an AJAX upload file to server. Firstly, we have to create html page that contain input type as file. Insert the script below below jquery cdn link.

var data1=new FormData();
data1.append("file",$("input[type=file]")[0].files[0]);
$.ajax({
         url:"your_url_upload_file",
         type:"POST",
         cache : false,
         contentType : false, //Important
         processData : false, //Important
         data : data1,
         success : function(status) { }
});

The line number 2 append to file attribute, so you can get data file via the attribute on server.

Here is the full code snippet should be:

Javascript  JQuery 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him