angular - 如何获取图像并在我的页面中显示 angular 6
问题描述
我正在将上传器中的图像保存到数据库中,因此它对我来说非常完美,但问题是我是否想在我的网站上显示该图像,那么我该如何显示它们,并且图像路径也应该显示为src="http://www.example.com/image.png"
. 请给我任何示例/解决方案。
TS
imageUrl: string = "/assets/img/default-image.png";
fileToUpload: File = null;
constructor(private imageService: ImageUploadService) { }
handleInputFile(file: FileList){
this.fileToUpload = file.item(0);
var reader = new FileReader();
reader.onload = (event:any) => {
this.imageUrl = event.target.result;
}
reader.readAsDataURL(this.fileToUpload);
}
OnSubmit(Caption,Image){
this.imageService.postFile(Caption.value, this.fileToUpload).subscribe(
data => {
console.log("Done");
Caption.value = "";
Image.value = "";
}
);
}
服务.Ts
postFile(caption: string, fileToUpload: File){
const endpoint = 'http://localhost:3343/api/UploadImage';
const formData: FormData = new FormData();
formData.append("Image", fileToUpload, fileToUpload.name);
formData.append("Caption", caption);
return this.http.post(endpoint,formData);
}
HTML
<form #imageForm="ngForm" (ngSubmit)="OnSubmit(Caption,Image)">
<div class="input-field col s12">
<input type="text" #Caption ngModel name="Caption" id="Caption">
<label for="Caption">Caption</label>
</div>
<img [src]="imageUrl" style="width:200px;height:200px;">
<input type="file" #Image accept="Image/*" (change)="handleInputFile($event.target.files)">
<button class="btn waves-effect waves-light" type="submit">Submit
<i class="material-icons right">send</i>
</button>
</form>
解决方案
我正在分享我的项目中的代码。在这个例子中,
我有一个 API 调用它customerNumber
返回我的blob
类型数据。然后我将这些数据从服务返回到我的组件,在我的组件端,我将该数据作为 blob 并Image
使用this.createImageFromBlob
函数将其转换为类型。最后,我通过在标签中使用imageToShow.photo
变量在模板端显示该图像<img [src]="imageToShow.photo">
。
我的 service.ts 方面,
getCustomerImages(customerNumber: number, type: string): Observable<File> {
let result: Observable<any> = this.http
.get(`${this.apiBaseUrl}csbins/Customer/${customerNumber}/images`, { responseType: "blob" });
return result;
}
我的 component.ts 方面,
getCustomerImages() {
this.isImageLoading = true;
this.getCustomerImagesSubscription = this.getCustomerService.getCustomerImages(this.refNo).subscribe(
data => {
this.createImageFromBlob(data);
this.isImageLoading = false;
},
error => {
this.isImageLoading = false;
});
}
并在您的组件中将 blob 转换为图像,
createImageFromBlob(image: Blob) {
let reader = new FileReader();
reader.addEventListener("load",
() => {
this.imageToShow.photo = reader.result;
},
false);
if (image) {
if (image.type !== "application/pdf")
reader.readAsDataURL(image);
}
}
和我的 template.html 方面,
<ng-template [ngIf]="imageTypeShow">
<ng-template [ngIf]="imageToShow.photo">
<img [src]="imageToShow.photo" class="img-thumbnail" *ngIf="!isImageLoading;">
</ng-template>
</ng-template>
如果您有任何不明白的地方,请随时询问。
推荐阅读
- node.js - Firebase Cloud 功能上的 Firebase 令牌刷新
- linux - 从 cron 运行时远程端口转发断开连接
- mysql - MYSQL 查询:从同一个表中查找与通过第二个表确定的条件匹配的相关条目
- google-cloud-dataflow - 获取来自数千个传感器的数据的平均值
- windows - 如何使用 ıp 或用户名获取计算机名称远程计算机?
- javascript - 得到“错误 TS2304:找不到名称”
'` 与 ts-node 编译 - amazon-web-services - 我应该为所有应用程序创建一个实例,还是应该为每个应用程序使用一个?
- php - 如果 WooCommerce 订单包含来自某个类别的商品,则设置自定义状态
- python - 当 Debug = False 时,Django 应用程序在本地主机上获取服务器错误 500
- php - PHP/AJAX:将 BLOB 插入 MySQL 数据库