2

I use YII2 + /kartik-v/bootstrap-fileinput plugin. I need to upload file when user just drop it into upload input. Make it by ajax request. When I set "showUpload" option to false, and set "uploadUrl" with ajax action, no request to server happens on change file event.

<?= $form->field($model, 'logotype')->widget(FileInput::class, [
    'options'       => [ 'accept' => 'image/*' ],
    'pluginOptions' => [
    'showCaption' => false,
    'showRemove'  => false,
    'showUpload'  => false,
    'browseClass' => 'btn btn-primary btn-block',
    'browseIcon'  => '<i class="glyphicon glyphicon-camera"></i> ',
    'uploadUrl'=> '/module/controller/action'
],
]); ?>

I expect the form to create ajax POST on the server with my file in data when user select image and without press "upload" button additionally. Is it possible to make it using this plugin? Or I should write a custom handler onchange event? Maybe there is some kind of callback in plugin options.

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
Valik Tralik
  • 69
  • 1
  • 12

2 Answers2

2

You can use the change event to trigger the upload manually when the file is selected and to trigger the ajax upload you should call the upload method.

See the below code

$('#input-id').on('change', function(event) {
    $(this).fileinput('upload');
});

Hope it helps

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
1

For me "filebatchselected" event works.

Originally https://plugins.krajee.com/file-input-ajax-demo/6

$("#projectplan-plan_file").on('filebatchselected', function(event) {
    $(this).fileinput('upload');
});
Prahlad
  • 716
  • 8
  • 17