How can I display an image using Typescript and Angular 4

后端 未结 2 1277
南旧
南旧 2020-12-28 09:54

I am very new to typescript and web development in general so I am sorry if my code is terrible. Anyways, I am having an issue displaying a \"preview image\" for a profile p

2条回答
  •  余生分开走
    2020-12-28 10:30

    You achive this by this simple function :

    Template :

     

    Component :

    onSelectFile(event) { // called each time file input changes
        if (event.target.files && event.target.files[0]) {
          var reader = new FileReader();
    
          reader.readAsDataURL(event.target.files[0]); // read file as data url
    
          reader.onload = (event) => { // called once readAsDataURL is completed
            this.url = event.target.result;
          }
        }
    }
    

    Here is the working example of it, please check this out:

    WORKING DEMO

提交回复
热议问题