Jquery file upload plugin: how to validate files on add?

后端 未结 6 2182
予麋鹿
予麋鹿 2020-12-13 15:47
$(\'#fileupload\')
    .fileupload({
        acceptFileTypes: /(\\.|\\/)(jpg)$/i
    })
    .on(\'fileuploadadd\', function (e, data) {
        console.log(data.file         


        
6条回答
  •  失恋的感觉
    2020-12-13 16:33

    This is what I did and it worked for me for newer versions: I created one validation per type of file and its size.

    $("#fileUploadArea").fileupload({
            dataType: 'json',
            url:'${upload}',
            multiple:true,
            autoSubmit:false,
            maxNumberOfFiles: Number('${quantidadeMaximaArquivosUpload}'),
            dynamicFormData: function()
            {
                var data ={ 
                        idEntidadeEmpresarial: $('#idEntidadeEmpresarial').val(),
                        idDominioFamilia: $('#idDominioFamilia').val(),
                        idSubgrupo: $('select[id^="subgrupo_').map(function(){return $(this).val();}).get(),
                        descricao: $('#descricao').val(),
                        validade: $('#validade').val()
                }
                return data;
            },
            headers: {
                Accept: "application/json"
            },
            accept: 'application/json',        
            imageMaxWidth: 800,
            imageMaxHeight: 800,
            imageCrop: true ,
            stop: function(){
                $.unblockUI();
                if($('#fechar').is(":checked")){
                    window.close();
                }else{
                    $('select[id^="subgrupo_').each(function(){
                        $(this).val('');
                        $(this).trigger("chosen:updated");
                    })
                    $('#validade').val('');
                    $('#descricao').val('');
                    $('#sucesso').html('');
                    $('#sucesso').append('

    '); $('#sucesso').show(); } }, error: function(files,status,errMsg) { $('#erro').html(''); $('#erro').append('

    '+errMsg+'

    '); $('#erro').show(); }, acceptFileTypes : ${listaExtensaoPermitidas} }).on('fileuploadprocessalways', function (e, data) { var currentFile = data.files[data.index]; var tamanho = currentFile.size; var extensao = currentFile.name.substring(currentFile.name.lastIndexOf(".") +1,currentFile.name.length); if(hashExtensao.get(extensao.toUpperCase()) < tamanho){ alert("file type max size "+hashExtensao.get(extensao.toUpperCase()); data.abort(); } });

提交回复
热议问题