Questions with File Upload

Asked

Viewed 448 times

1

My question is regarding File Upload using the http://plugins.krajee.com/file-input

I have the following JS code

$('#arquivoCliente').fileinput({
            language: 'pt-BR',
            uploadAsync: false,
            showPreview: false,
            maxFileSize: 200,
            uploadUrl: 'upload.php',
            allowedFileExtensions : ['PDF', 'pdf']
            uploadExtraData: function() {
                return {
                    id: tempIDCliente
                };
            }
        });

My php is all ok, it’s saving the file and everything. What I can’t do is. As you can see my code is set to accept only the PDF extension. If the user chooses another extension, the send button is disabled.

However this option is only working if I comment on the uploadUrl line, as in the example below.

$('#arquivoCliente').fileinput({
            language: 'pt-BR',
            uploadAsync: false,
            showPreview: false,
            maxFileSize: 200,
            // uploadUrl: 'upload.php',
            allowedFileExtensions : ['PDF', 'pdf']
            uploadExtraData: function() {
                return {
                    id: tempIDCliente
                };
            }
        });

Not commented is like this Não comentado

Commented on like this Comentado

  • But what do you want? Just remembering that the Javascript of this plugin works like this!

  • Hi @Virgilionovic I did the following, disabled in fileuploaderror, $('#fileCliente'). on('fileuploaderror', Function(Event, data, msg) { var form = data.form, files = data.files, extra = date.extra, Response = data.Response, Reader = data.Reader; console.log('File upload error'); // get message alert(msg, 'Danger'); $('.fileinput-upload-button').attr('disabled', 'disabled'); }); ?

  • So did you manage to solve? if yes put as answer!

  • Actually I don’t know if that’s so. It worked, but I don’t know if it was a great gambit.

  • Maybe it’s not guambiarra, maybe it’s a bug, and it’s bypassed!

  • 1

    If it has repository on github maybe open an Issue and tell them the problem is a good one! https://github.com/kartik-v/bootstrap-fileinput

  • 1

    I’m going to put my resolution as an answer here and do this.

  • 1

    Thank you @Virgilionovic

Show 3 more comments

1 answer

1


I don’t know if it’s right, but I managed to get around it this way.

$('#arquivoCliente').fileinput({
            language: 'pt-BR',
            uploadAsync: true,
            showPreview: false,
            maxFileSize: 200,
            uploadUrl: 'upload.php',
            allowedFileExtensions : ['PDF', 'pdf'],
            uploadExtraData: function() {
                return {
                    id: tempIDCliente
                };
            }
        });

        $('#arquivoCliente').on('fileuploaded', function(event, data, previewId, index) {
            var form = data.form, files = data.files, extra = data.extra,
                response = data.response, reader = data.reader;
            console.log('File uploaded triggered');
            $('#arquivoCliente').fileinput('clear'); // Essas três linhas uso para atualizar o campo após o update
            $('#arquivoCliente').fileinput('refresh');
            $('#arquivoCliente').fileinput('enable');


        });

        //aqui testa se o arquivo é PDF e dentro do tamanho máximo estipulado

        $('#arquivoCliente').on('fileuploaderror', function(event, data, msg) {
            var form = data.form, files = data.files, extra = data.extra,
                response = data.response, reader = data.reader;
            console.log('File upload error');
           alerta(msg, 'danger');
           $('.fileinput-upload-button').attr('disabled', 'disabled'); // Desabilito o botão de upload manualmente
        });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.