Upload Many

Upload Many

febra69febra69 Posts: 5Questions: 3Answers: 0

Good afternoon

In the uploadMany method

For the function on an error

Example
I put 100 files to upload, in the middle of them I put a file that already exists in the database, then I return to saying that the file already exists.
However, I want the remaining files to be uploaded, and for all errors to appear together.

At this point, what happens is that it loads the first files, and then stops.
And only the last error appears.

Supposedly when uploading several files we should be able to upload all of them except for the ones that give an error.

editorCreate = new $.fn.DataTable.Editor( {
                    ajax: "path/to/file",
                    table: "#archive",
                    fields: [ 
                        {
                              label: "Categoria:"
                            , name: "categoria.id"
                            , type: "select"
                        }, {
                              label: "Anexos:"
                            , name: "files[].id"
                            , type: "uploadMany"
                            , display: function ( fileId, counter ) {
                                return '<a href="' + editorCreate.file( 'files', fileId ).web_path + '" target="_blank">' + editorCreate.file( 'files', fileId ).filename + '</a>';
                            }
                        }
                    ],
                    i18n: json.editor
                })

                editorCreate.on( 'preSubmit', function ( e, o, action ) {
                    if ( action !== 'remove' )
                    {

                        var nome   = this.field('categoria.id')
            
                        if ( !nome.isMultiValue() && !nome.val() )
                            nome.error( 'Campo obrigatório!' )
            
                        if ( this.inError() )
                            return false

                    }

                })
                
                editorCreate.on( 'preUpload', function ( e, fieldName, file, ajaxData ) {

                    if ( file.size > 5242880 )
                    {

                        editorCreate.field( fieldName ).error('Tamanho máximo por ficheiro é 5Mb!')
                        return false

                    }
                    else if ( file.type != 'application/pdf')
                    {

                        editorCreate.field( fieldName ).error('Somente ficheiros em PDF!')
                        return false

                    }
                    else
                        editorCreate.field( fieldName ).error('')

                    ajaxData.append('id_categoria', editorCreate.field('categoria.id').val());

                })
                
                editorEdit = new $.fn.DataTable.Editor( {
                    ajax: "path/to/file/",
                    table: "#archive",
                    fields: [ 
                        {
                              label: "Categoria:"
                            , name: "categoria.id"
                            , type: "select"
                        }
                    ],
                    i18n: json.editor
                })

                editorEdit.on( 'preSubmit', function ( e, o, action ) {
                    if ( action !== 'remove' )
                    {

                        var nome   = this.field('categoria.id')
            
                        if ( !nome.isMultiValue() && !nome.val() )
                            nome.error( '{{ "Campo obrigatório!"|trans }}' )
            
                        if ( this.inError() )
                            return false

                    }

                })

                var archive = $('#archive').DataTable({
                    ajax: {
                        url: "path/to/file",
                        type: "POST",
                    },
                    dom: `<'row'<'col-md-6'B><'col-md-6 text-right'l>>
                            <'row'<'col-md-12't>>
                            <'row'<'col-md-6'i><'col-md-6'p>>`,
                    columnDefs: [
                        { orderable: false, targets: -1 }
                    ],
                    columns: [
                        { data: "id"            , className: "text-nowrap text-left "                                                                                                                                       },
                        { data: "nome"          , className: "text-nowrap text-left "                                                                                                                                       },
                        { data: "categoria.nome", className: "text-nowrap text-left ", editField: "categoria.id"                                                                                                            },
                        { data: "estado.nome"   , className: "text-nowrap text-left ", editField: "estado.id"                                                                                                               },
                        { data: "data_add"      , className: "text-nowrap text-right"                                                                                                                                       },
                        { data: "total_docs"    , className: "text-nowrap text-right"                                                                                                                                       },
                        { data: "caminho"       , className: "text-nowrap text-right", render: function (d) { return '<a href="' + d + '" target="_blank" class="btn btn-info btn-sm"><i class="fa-fw fas fa-eye"></i></a>'}},
                    ],
                    buttons: [
                        { extend: 'create', className: 'btn-success', editor: editorCreate },
                        { extend: 'edit'  , className: 'btn-warning', editor: editorEdit   },
                    ],
                    order: [[5, 'asc'], [0, 'desc']],
                    lengthMenu: [[10, 25, 50, 100], [10, 25, 50, 100]],
                    rowCallback: function ( row, data ) {
                        $('input.editor-contem-qr', row).prop( 'checked', data.contem_qr == 1 ){{ readonly is defined ? '.attr("disabled", true)' }}
                        
                        $('td:eq(6)', row).prepend('<button type="button" class="btn btn-secondary js-editor-qr  btn-sm mr-1" data-id="' + data.id + '" data-caminho="' + data.caminho + '"><i class="fa-fw fas fa-crop"></i></button>')
                        $('td:eq(6)', row).prepend('<button type="button" class="btn btn-warning abrir-leitor-qr btn-sm mr-1" data-id="' + data.id + '" ><i class="fa-fw fas fa-camera"></i></button>')
                        
                        if(data.estado.id == 1)
                            $('td:eq(6)', row).prepend('<button type="button" class="btn btn-danger js-delete-archive btn-sm mr-1" data-id="' + data.id + '" ><i class="fa-fw fas fa-trash"></i></button>')

                    },
                    initComplete: function () {
                        this.api().buttons().container()
                            .appendTo($('#archive_wrapper .col-md-6:eq(0)'))
                    }
                })

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    At this time you would need to not return an error for the file that is causing the issue. The Editor upload will only continue on to the next file if there is no error reported. If there is an error, then it will break out of the upload loop.

    Allan

  • febra69febra69 Posts: 5Questions: 3Answers: 0
    edited February 2022

    OK,
    Due to the type of project that I am developing, I need it to continue and to present all the errors that are presented
    I've managed to get the files to continue uploading

    Example
    The client uploads 100 files.
    There are several checks, 2 of which are performed after sending the file via ajax.
    Among the 100, only 2 do not group conditions to upload, for example, load 40 and so on.
    Then how does the customer know which ones were loaded or not?
    Because there is also a check in ajax that is if the file has already been loaded.

    I needed the errors to be visible until opening the modal again, part informs the customer why the file was not damaged

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Then how does the customer know which ones were loaded or not?

    Could you hold it in a session variable and then report it at the end? Or perhaps put the error message into a custom property ("fileError" would do) since Editor specifically looks for the error property. You could then display that information via the uploadXhrSuccess event.

    Allan

Sign In or Register to comment.