File Upload with Angular Material

后端 未结 12 1053
抹茶落季
抹茶落季 2021-01-30 15:48

I\'m writing an web app with AngularJS and angular-material. The problem is that there\'s no built-in component for file input in angular-material. (I feel that file uploading d

相关标签:
12条回答
  • 2021-01-30 16:29
    html:
        <div class="upload">
            <span>upload image</span>
            <input
              #Image
              type="file"
              (change)="handleFileInput($event.target.files)"
              accept=".jpg,.svg,.png,.jpeg"
            />
            <img
              width="100%"
              height="100%"
              *ngIf="imageUrl"
              [src]="imageUrl"
              class="image"
            />
        </div>
    

    app.component.ts

    export class AppComponent {
      options = [{ value: "This is value 1", checked: true }];
      statuses = ["control"];
    
      // name = "Angular";//
      fileToUpload: any;
      imageUrl: any;
      handleFileInput(file: FileList) {
        this.fileToUpload = file.item(0);
    
        //Show image preview
        let reader = new FileReader();
        reader.onload = (event: any) => {
          this.imageUrl = event.target.result;
        };
        reader.readAsDataURL(this.fileToUpload);
      }
    }
    
    0 讨论(0)
  • 2021-01-30 16:31

    Adding to all the answers above (which is why I made it a community wiki), it is probably best to mark any input<type="text"> with tabindex="-1", especially if using readonly instead of disabled (and perhaps the <input type="file">, although it should be hidden, it is still in the document, apparently). Labels did not act correctly when using the tab / enter key combinations, but the button did. So if you are copying one of the other solutions on this page, you may want to make those changes.

    0 讨论(0)
  • 2021-01-30 16:36

    Nice solution by leocaseiro

    <input class="ng-hide" id="input-file-id" multiple type="file" />
    <label for="input-file-id" class="md-button md-raised md-primary">Choose Files</label>
    

    View in codepen

    0 讨论(0)
  • 2021-01-30 16:36

    Another example of the solution. Will look like the following

    CodePen link there.

      <choose-file layout="row">
        <input id="fileInput" type="file" class="ng-hide">
        <md-input-container flex class="md-block">
          <input type="text" ng-model="fileName" disabled>
          <div class="hint">Select your file</div>
        </md-input-container>
        <div>
          <md-button id="uploadButton" class="md-fab md-mini">
            <md-icon class="material-icons">attach_file</md-icon>
          </md-button>
        </div>
      </choose-file>   
    
    .directive('chooseFile', function() {
        return {
          link: function (scope, elem, attrs) {
            var button = elem.find('button');
            var input = angular.element(elem[0].querySelector('input#fileInput'));
    
            button.bind('click', function() {
              input[0].click();
            });
    
            input.bind('change', function(e) {
              scope.$apply(function() {
                var files = e.target.files;
                if (files[0]) {
                  scope.fileName = files[0].name;
                } else {
                  scope.fileName = null;
                }
              });
            });
          }
        };
      });
    

    Hope it helps!

    0 讨论(0)
  • 2021-01-30 16:37

    Another hacked solution, though might be a little cleaner by implementing a Proxy button:

    HTML:

    <input id="fileInput" type="file">
    <md-button class="md-raised" ng-click="upload()">
      <label>AwesomeButtonName</label>
    </md-button>
    

    JS:

    app.controller('NiceCtrl', function ( $scope) {
      $scope.upload = function () {
        angular.element(document.querySelector('#fileInput')).click();
      };
    };
    
    0 讨论(0)
  • 2021-01-30 16:42

    I've join some informations posted here and the possibility of personalize components with Angular Material and this is my contribution without external libs and feedback with the name of the chosen file into field:

    HTML

    <mat-form-field class="columns">
        <mat-label *ngIf="selectedFiles; else newFile">{{selectedFiles.item(0).name}}</mat-label>
        <ng-template #newFile>
            <mat-label>Choose file</mat-label>
        </ng-template>
        <input matInput disabled>
        <button mat-icon-button matSuffix (click)="fileInput.click()">
            <mat-icon>attach_file</mat-icon>
        </button>
        <input hidden (change)="selectFile($event)" #fileInput type="file" id="file">
    </mat-form-field>
    

    TS

    selectFile(event) {
        this.selectedFiles = event.target.files;
    }
    
    0 讨论(0)
提交回复
热议问题