gpt4 book ai didi

angular - 上传后立即以 Angular 显示上传的图像

转载 作者:行者123 更新时间:2023-12-03 16:03:29 24 4
gpt4 key购买 nike

在使用 angular 输入文件上传后,我试图立即显示上传的图像。
我试图从 typescript 文件中获取 url 以在 html 文件中显示它:

<img src={{url}}>

我有这样的文件:
html:
  <input type="file" (change)="onFileChanged($event)" >
ts:
  onFileChanged(event) {
this.selectedFile = event.target.files[0]
console.log(this.selectedFile)
}
我该怎么做才能获取图像并立即显示?

最佳答案

onFileChanged(event) {
const files = event.target.files;
if (files.length === 0)
return;

const mimeType = files[0].type;
if (mimeType.match(/image\/*/) == null) {
this.message = "Only images are supported.";
return;
}

const reader = new FileReader();
this.imagePath = files;
reader.readAsDataURL(files[0]);
reader.onload = (_event) => {
this.url = reader.result;
}
}

关于angular - 上传后立即以 Angular 显示上传的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58746058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com