klren0312 / daliy_knowledge

知识积累,正确使用方式是watch
21 stars 4 forks source link

js判断上传的图片是否是符合规定的长宽 #838

Open klren0312 opened 4 months ago

klren0312 commented 4 months ago
const checkImgWidthHeight = (theFile: File, width: number, height: number) => {
  return new Promise((resolve) => {
    const reader = new FileReader()
    reader.onload = (e) => {
      const img = new Image()
      img.onload = () => {
        if (img.width === width && img.height === height) {
          console.log('图片尺寸正确')
          resolve(true)
        } else {
          resolve(false)
        }
      }
      img.src = e.target?.result as string
    }
    reader.readAsDataURL(theFile)
  })
}