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

Sovary October 1, 2021 442
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

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him