This time I will bring you a file upload extension made with jQuery. What are the precautions for using jQuery to make a file upload extension? The following are Let’s take a look at practical cases.
Go directly to the code:
/* *jquery.ajaxUpload.js */jQuery.extend({ ajaxFileUpload: function(s) { s = jQuery.extend({}, jQuery.ajaxSettings, s); s.type = "POST"; var f = new FormData(); for (var k in s.data) { f.append(k, s.data[k]); } if (s.fileElementId) { if (!jQuery("#" + s.fileElementId).attr("multiple")) { f.append(s.fileElementId, jQuery("#" + s.fileElementId).get(0).files[0]); } else { var fs = jQuery("#" + s.fileElementId).get(0).files; for (var i = 0; i < fs.length; i++) { f.append(s.fileElementId + "[]", fs[i]); } } } s.processData = s.contentType = false; s.data = f; jQuery.ajax(s); } });
Instructions for use: The principle of the script is to process the data before using $.ajax. The usage method is the same as $.ajax. It just adds fileElementId AttributeUsed to identify the id of the input type="file" node.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Related reading:
Website that uses nodejs for introduction
How to write a simulator with JS
How to create a 1px border effect on the mobile terminal
The above is the detailed content of File upload extension made with jQuery. For more information, please follow other related articles on the PHP Chinese website!