如何编码图像数组,然后发布它们?

时间:2019-07-16 13:32:08

标签: angular typescript post base64

我尝试对图像列表进行编码,然后通过发布请求将它们发送到rest api。 即使经过大量的尝试和失败,我也无法保证编码后会触发“ post”

我不确定是否相关,但我应该说该应用程序是在Android设备上运行的离子应用程序

这是代码。触发后,blob列表为空,但是我在日志中确实得到了编码图像。

IsReadOnly

1 个答案:

答案 0 :(得分:1)

首先,您没有初始化lesBlobs,只是对其进行了声明。您需要将其更改为let lesBlobs: string[] = [];

private async loadImages(paths: string[]) {
let lesBlobs :string[];
paths.forEach(async (unPath) => {
  let img = await this.encodeImage(unPath);
  console.log ("img :" + img );
  lesBlobs.push(img);
});
console.log("returning les blobs");
return lesBlobs;

}

除此之外,您的lesBlobs很可能仍会返回一个空数组,因为forEach是一个异步函数。最简单的方法是将其更改为

for(const unPath of paths) {
  let img = await this.encodeImage(unPath);
  console.log ("img :" + img );
  lesBlobs.push(img);
}