I've found this JavaScript code that allows to upload files asynchronously but the are some parts that I don't understand. any pointers or explanation highly appreciated - thanks in advance,
// Ajax File upload with jQuery and XHR2
// Sean Clark http://square-bracket.com
// xhr2 file upload
// data is optional
$.fn.upload = function(remote, data, successFn, progressFn) {
// if we dont have post data, move it along
if (typeof data != "object") {
progressFn = successFn;
successFn = data;
}
//What is doing here?
return this.each(function() {
if ($(this)[0].files[0]) {
var formData = new FormData();
formData.append($(this).attr("name"), $(this)[0].files[0]);
<input type="file">probably) and fetches the file to upload.FormDatais a way to send form asynchronously.$(this)[0]is a pretty good indicator that the author of this code doesn't really have a very deep understanding of things. It's effectively identical to just referencingthis, but is wasteful with memory and CPU.