简体   繁体   English

获取图像数组的宽高 angular typescript

[英]Get the width and height of an array of images angular typescript

I am uploading multiple images and before I upload I want to get the width and height of each image.我正在上传多张图片,在上传之前我想获取每张图片的宽度和高度。 The problem is that my function only returns the width and height of the first image only.问题是我的 function 只返回第一张图片的宽度和高度。 Here's my code:这是我的代码:

async getWidthAndHeight(files: any) {
    const newPromise = [files].map((file: any, i: any) => {
      return new Promise((resolve, reject) => {
        var reader = new FileReader();
        reader.readAsDataURL(file[i]);
        reader.onload = async (e: any) => {
          var image = new Image();
          image.src = e.target.result;
          image.onload = async () => {
            this.imageWidth = image.width;
            this.imageHeight = image.height;
            this.sizeObj = {
              width: this.imageWidth,
              height: this.imageHeight
            };
            resolve(this.sizeObj)
          }
        }
      })
    })
    const final = await Promise.all(newPromise);
    return final
  }

The return value is an array with the dimensions of the first image alone.返回值是一个仅包含第一张图像尺寸的数组。 I want the function to return the dimensions of all images.我希望 function 返回所有图像的尺寸。 Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks!谢谢!

Considering your files as FileList use this to create array of promises and use file instead of file[i]将您的文件视为 FileList 使用它来创建承诺数组并使用 file 而不是 file[i]

const newPromise = Array.from(files).map(file => {
  console.log(file);
  // your code
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM