jquery支持AJAX多文件上传接口php源码

所属分类: 网页特效-图片特效&上传    2023-10-29 08:21:59

jquery支持AJAX多文件上传接口php源码 ie兼容6
反馈问题  查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

jquery支持AJAX多文件上传接口php源码(共20个文件)

    • index.html

使用方法

infoPhp = [];
postfiles = [];
formData = new FormData();

$(function() {

    $.fn.swallow = function (options){

    //Config path by default
        var pat = $.extend({
            imgPath             : "img/",
            phpPath             : "php/",         
            targetPath          : "nest/"
        }, options);   

    //Search on the PHP server configuration items
        $.ajax({url:pat.phpPath + 'info.php',async:false,
            success: (s) => {infoPhp.push(JSON.parse(s));},
            error: () => {alert('require ==> info.php');return false;},
        })

    //Config options user
        var ext = $.extend({ 
            swallowTag          : false,   
            targetTag           : infoPhp[0]['tag_swallow'],            
            method              : "POST",
            enctype             : "multipart/form-data",
            acceptFiles         : "*",
            maxFileSize         : infoPhp[0]['upload_max_filesize'],
            maxFileCount        : infoPhp[0]['max_file_uploads'],
            postMaxSize         : infoPhp[0]['post_max_size'],
            maxImgQuality       : 100,
            defaultImg          : "file.png",
            labelInput1         : "Add your files",
            labelInput2         : "Number of files",
            labelName           : "Name",
            labelType           : "Type file",
            labelModified       : "Modified",
            labelSize           : "Size",
            labelSaveBtn        : "Save",
            labelDeleteBtn      : "Delete",
            labelLoadingBtn     : "Loading...",
            labelSuccess        : "File upload successful",
            labelAcceptFiles    : "Accepted formats",
            labelMaxFileSize    : "Max size accept",
            labelMaxFileCount   : "Max files accept",
            labelAllDenied      : "Denied !",
            onSuccess           : function (response, statut) {},
            onError             : function (response, statut, erreur) {}
        }, options);

        

    //Form input bootstrap 4
        $(this).append('\
        <div class="row">\
            <div class="col-12 mb-2">\
                <form method="post" enctype="'+ ext.enctype +'">\
                    <div class="alert alert-danger d-none fade show" role="alert" id="ErrUpload"></div>\
                        <div class="custom-file">\
                            <input type="file" class="custom-file-input" id="swallow" name="swallow[]" multiple>\
                            <label class="custom-file-label" for="swallow">'+ ext.labelInput1 +'</label>\
                        </div>\
                        <div class="mt-1">\
                            <button class="btn btn-block btn-success d-none" id="SaveFiles" type="button">'+ ext.labelSaveBtn +'</button>\
                            <button class="btn btn-block btn-info WaitSpinner d-none" disabled><span class="spinner-grow spinner-grow-sm"></span> '+ ext.labelLoadingBtn +'</button>\
                        </div>\
                </form>\
            </div>\
            <div class="col-12 mt-2">\
                <ul class="list-unstyled gallery"></ul>\
                <div class="toast d-none" aria-atomic="true" data-delay="2000" id="SuccUpload">\
                <div class="toast-header">\
                <img src="'+ pat.imgPath + ext.defaultImg +'" class="rounded mr-2" height="15" width="15">\
                <strong class="mr-auto">Swallow</strong>\
                <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">\
                  <span aria-hidden="true">&times;</span>\
                </button>\
                </div>\
                <div class="toast-body">'+ ext.labelSuccess +'</div>\
                </div>\
            </div>\
        </div>\
        ')

    //Tag file or multiple files
        $("#swallow").on("change", function(e) {

            const files = e.currentTarget.files;
            $("#ErrUpload").empty().removeClass('d-block').addClass('d-none');
            $("#SaveFiles").removeClass('d-block').addClass('d-none');

        //Defaut save formData
            $.each($(this)[0].files, function(i, file) {formData.append('swallow[]', file);});

            Object.keys(files).forEach(i => {

                const file = files[i];

            //Option type files
                if(ext.acceptFiles != "*"){
                    afAccept = false;
                    afArray = ext.acceptFiles.split(',');
                    afArray.forEach(element => {if(file.type === element){afAccept = true;}});
                        if(afAccept === false){
                            $("#ErrUpload").removeClass('d-none').addClass('d-block')
                            $("#ErrUpload").html(ext.labelAcceptFiles + ' : <b>' + ext.acceptFiles + ' :</b> <span class="text-danger">'+ file.name+ ' </span><b>'+ ext.labelAllDenied +'</b>');                        
                        }
                }else{afAccept = true;}

            //Option max size
                if(parseInt(ext.maxFileSize) > parseInt(file.size)){mfsAccept = true;}else{
                    $("#ErrUpload").removeClass('d-none').addClass('d-block')
                    $("#ErrUpload").html(ext.labelMaxFileSize + ' : <b>' + Math.round((ext.maxFileSize / 1000000)*100)/100 + 'Mo :</b> <span class="text-danger">'+ file.name+ ' </span><b>'+ ext.labelAllDenied +'</b>');                    
                    mfsAccept = false;
                }

            //Option max files
                if(parseInt(postfiles.length) < parseInt(ext.maxFileCount)){mfcAccept = true;}else{
                    $("#ErrUpload").removeClass('d-none').addClass('d-block')
                    $("#ErrUpload").html(ext.labelMaxFileCount + ' : <b>' + ext.maxFileCount);                    
                    mfcAccept = false;
                }

                var doublon = Object.keys(postfiles).some(function(k) {return postfiles[k] === file.name;});

                if(doublon === false && afAccept === true && mfsAccept === true && mfcAccept === true){
                    
                    postfiles.push(file.name);

                    const reader = new FileReader();
                    
                    reader.onload = (e) => {

                        var $LastModified = new Date(file.lastModified).toISOString().split('T')[0]
                        var ReaderResultat= pat.imgPath + ext.defaultImg;                        
                        Object.keys($mimeType).forEach(m => {
                            if($mimeType[m].type === file.type){
                                switch ($mimeType[m].source) {
                                    case 'img'          :ReaderResultat = reader.result;break;
                                    case 'no-img' :ReaderResultat;break;
                                    default          :ReaderResultat = pat.imgPath + $mimeType[m].source;
                                }
                            }
                        })
              
                        $(".gallery").append('\
                            <li class="border border-right-0 border-top-0 border-success shadow py-1 mb-1" style="border-width:3px !important;">\
                                <img src="'+ ReaderResultat +'" class="rounded p-2" height="100" width="100">\
                                <div class="p-2">\
                                <h6 class="text-break">'+ ext.labelName +': '+ file.name+'</h6>\
                                <h6 class="text-break">'+ ext.labelType +': '+ file.type +'</h6>\
                                <h6 class="text-break">'+ ext.labelModified +': '+ new Date(file.lastModified).toISOString().split('T')[0] +'</h6>\
                                <h6 class="text-break">'+ ext.labelSize +': '+ Math.round((file.size / 1000000)*100)/100 +' Mo</h6>\
                                <button data-id="'+ file.name +'" class="btn btn-danger Erase">'+ ext.labelDeleteBtn +'</button>\
                                </div>\
                            </li>\
                        ')

                    }
                        
                    reader.readAsDataURL(file);
                }
                
            })

            if(postfiles.length === 0){
                $("#SaveFiles").removeClass('d-block').addClass('d-none');
                $(".custom-file-label").html(ext.labelInput1);
            }else{
                $("#SaveFiles").removeClass('d-none').addClass('d-block');
                $(".custom-file-label").html(ext.labelInput2 + ': ' + postfiles.length);
            }

        });
        
    //Delete File
        $("div ul").on("click", ".Erase", function(){

            DataId = $(this).attr('data-id');
            tmp_postfiles = [];

            Object.keys(postfiles).forEach(i => {
                if(DataId != postfiles[i]){tmp_postfiles.push(postfiles[i]);}
            })

            postfiles = tmp_postfiles;

            $(this).parent().parent().remove();

            if(postfiles.length === 0){
                $("#SaveFiles").removeClass('d-block').addClass('d-none');
                $("#ErrUpload").removeClass('d-block').addClass('d-none');
                $(".custom-file-label").html(ext.labelInput1);
            }else{
                $("#SaveFiles").removeClass('d-none').addClass('d-block');
                $(".custom-file-label").html(ext.labelInput2 + ': ' + postfiles.length);
            }

        

站长提示:
1. 平台上所有素材资源,需注册登录会员方能正常下载。
2. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励20K币
3. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
4. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 图片特效&上传

js响应式带缩略图点击切换的幻灯片特效代码

一款图片切换特效,单击缩略图可现实对应的大图。
  图片特效&上传
 276  

jquery自动生成图片模糊版本作为背景图片插件

一款很有趣的插件,通过使用图片的模糊版本,提取主题色来创建令人惊叹的图片背景。
  图片特效&上传
 250  

响应式带鼠标悬停特效的网格布局图片集特效代码

纯CSS创建了一个倾斜的网格图片展示布局,它按行和列排列图片,带鼠标悬停效果,鼠标悬停于某张图片上,可进行放大突出显示。在不同设备下自行调整布局大小,达到自适应效果。
  图片特效&上传
 154  

jquery拉窗帘手风琴图片切换特效代码

一款点击触发展开或隐藏的图片特效,有点像拉窗帘的效果,很有趣!
  图片特效&上传
 205  

评论数(1) 回复有机会获得K币 用户协议

    great0
    2023-12-12 15:59:09
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论