Removing file from multiple files uploader on button click when using HTML5 file input

后端 未结 5 595
一个人的身影
一个人的身影 2020-11-29 01:53

How to add remove button here like simple remove one by one in files queue like this

The reason why im not using free file upload plugins w

5条回答
  •  再見小時候
    2020-11-29 02:33

    I've done this before for my Dropzone. Feel free to adjust. This is from my Laravel app. You should focus on avatar_refresh_upload. Cut off unnecessary stuff and you're done.

    function avatar_refresh_upload() {
        var input = $('input#avatar[type=file]');
    
        input.replaceWith(input.val('').clone(true));
    
        $('#selected_file').html('{{ Lang::get('app.profile_avatar_select') }}');
        $('#avatar_refresh_upload').removeAttr('style');
    }
    
    $(document).ready(function ($) {
    
        $('input:file#avatar').change(function () {
            var file_name = $(this).val();
            if (file_name.length > 10) {
                file_name = file_name.substring(0, 10) + '...';
            }
            $('#selected_file').html('File "' + file_name + '" chosen');
            $('#avatar_refresh_upload').css('display', 'inline-block');
        });
    
        $('#avatar_refresh_upload').on('click', function () {
            avatar_refresh_upload();
        });
    
        @if ($user->avatar != '')
        $('#remove_avatar').change(function () {
    
            if ($(this).is(':checked')) {
    
                avatar_refresh_upload();
                $('#avatar').prop('disabled', true);
                $('#avatar_preview').css('opacity', '0.5');
                $('#avatar_upload_form_area').css('opacity', '0.5');
                $('#remove_avatar_info').show();
    
            } else {
    
                $('#avatar').prop('disabled', false);
                $('#avatar_preview').removeAttr('style');
                $('#avatar_upload_form_area').removeAttr('style');
                $('#remove_avatar_info').removeAttr('style');
    
            }
        });
        @endif
    
    });
    

    Making long story short - if you want to reset input file after you picked a file for upload but before submitting, you have to run:

    input.replaceWith(input.val('').clone(true));
    

提交回复
热议问题