Image preview before upload in angular 5

后端 未结 7 1695
逝去的感伤
逝去的感伤 2020-12-15 10:17

.html

  
  \"your
7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 10:25

    .html

    Update event attr and handler param for input. And you should use data binding for src attribute. Following will apply src if it's not null or undefined or hardcoded url ('http://placehold.it/180')

    
    your image
    

    .ts

    In component ts file (class) you should have property imageSrc which be used in view (html) and your function should be a method of that class

    ...
    imageSrc: string;
    ...
    constructor(...) {...}
    ...
    readURL(event: Event): void {
        if (event.target.files && event.target.files[0]) {
            const file = event.target.files[0];
    
            const reader = new FileReader();
            reader.onload = e => this.imageSrc = reader.result;
    
            reader.readAsDataURL(file);
        }
    }
    

提交回复
热议问题