Home > Web Front-end > JS Tutorial > body text

File upload extension made with jQuery

php中世界最好的语言
Release: 2018-03-10 15:50:24
Original
1204 people have browsed it

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);
    }
});
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template