Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. HTML Teil aus meinem Form für ein neues Horse:
  2. <div class="form-group">
  3.       <label for="photo"></label>
  4.       <input type="file"
  5.              id="photo"
  6.              (change)="handlePhotoUpload($event.target.files)">
  7.       <img [src]="horse.photo" alt="<photo of horse>"/>
  8.     </div>
  9.  
  10. TypeScript Teil für Foto zu String konvertieren:
  11. handlePhotoUpload(files: FileList) {
  12.     let photo = files.item(0);
  13.     let fileReader = new FileReader();
  14.     fileReader.readAsDataURL(photo);
  15.     fileReader.onload = this.setPhotoString.bind(this);
  16.   }
  17.  
  18.   setPhotoString(fileReader){
  19.     let photoString = fileReader.target.result;
  20.     this.horse.photo = photoString;
  21.   }
  22. Des mit dem bind hab i vom gore weil sonst mit "this" net auf den component kommst sondern aufn FileReader. Sonst kannst this.horse.photo net setzen.
  23.  
  24. Dann halt noch dem horse.ts an string fürs bild hinzufügen.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement