Typescript 在9中的方法中等待文件读取器
如果该方法接收到多个图像,则只添加阵列中的最后一个图像。显然需要等待reader.onload。 请建议可能的方法Typescript 在9中的方法中等待文件读取器,typescript,angular9,Typescript,Angular9,如果该方法接收到多个图像,则只添加阵列中的最后一个图像。显然需要等待reader.onload。 请建议可能的方法 for (let file of files) { var mimeType = file.type; this.fileType = mimeType.split('/')[1]; console.log(mimeType); if (mimeType.match(/image\/*/) == null) {
for (let file of files) {
var mimeType = file.type;
this.fileType = mimeType.split('/')[1];
console.log(mimeType);
if (mimeType.match(/image\/*/) == null) {
this.message = "Only images are supported.";
return;
}
var reader = new FileReader();
this.imagePath = files;
var productImage = new ProductImage;
console.log(0);
reader.readAsDataURL(file);
console.log(1);
reader.onload = (_event) => {
console.log(2);
this.imgURL = reader.result;
this.imgURLs.push(this.imgURL);
productImage.fileType = this.getMediaType(this.fileType);
productImage.fileContents = this.imgURL.toString().split(',')[1];
console.log(3);
}
this.productImageList.push(productImage);
console.log(productImage);
console.log(this.productImageList);
}