A slight modification to the DannYOs response. I made a jQuery plugin that you can invoke by entering a file to make it simpler. You just pass it your script load, then your success function, and then your progress function.
$.fn.upload = function(remote,successFn,progressFn) { return this.each(function() { var formData = new FormData(); formData.append($(this).attr("name"), $(this)[0].files[0]); $.ajax({ url: remote, type: 'POST', xhr: function() { myXhr = $.ajaxSettings.xhr(); if(myXhr.upload && progressFn){ myXhr.upload.addEventListener('progress',progressFn, false); } return myXhr; }, data: formData, cache: false, contentType: false, processData: false, complete : function(res) { if(successFn) successFn(res); } }); }); }
Using
$(".myFile").upload("upload.php",function(res) { console.log("done",res); },function(progress) { console.log("progress", progress); });
source share