Styling an input type=“file” button

后端 未结 30 2088
终归单人心
终归单人心 2020-11-21 11:50

How do you style an input type=\"file\" button?

30条回答
  •  野性不改
    2020-11-21 12:18

    This week I also needed to custom the button and display the selected file name aside it, so after reading some of the answers above (Thanks BTW) I came up with the following implementation:

    HTML:

    {{fileName}}

    CSS

       input[type='file'] {
        color: #a1bbd5;
        display: none;
    
    }
    
    .custom-file-upload {
        border: 1px solid #a1bbd5;
        display: inline-block;
        padding: 2px 8px;
        cursor: pointer;
    }
    
    label{
        color: #a1bbd5;
        border-radius: 3px;
    }
    

    Javascript (Angular)

    app.controller('MainCtrl', function($scope) {
    
            $scope.fileName = 'No file chosen';
    
              $scope.onFileSelect = function ($files) {
              $scope.selectedFile = $files;
              $scope.fileName = $files[0].name;
        };
    });
    

    Basically I'm working with ng-file-upload lib, Angular-wise I'm binding the filename to my $scope and giving it the initial value of 'No file chosen', I'm also binding the onFileSelect() function to my scope so when a file gets selected I'm getting the filename using ng-upload API and assign it to the $scope.filename.

提交回复
热议问题