ant design vue 獲取上傳圖片的像素


獲取圖片的像素大小,使用上傳文件之前的鈎子 beforeUpload,參數為上傳的文件。

<a-upload
      name="file"
      list-type="picture-card"
      class="avatar-uploader"
      :show-upload-list="false"
      :multiple="false"
      :before-upload="beforeUpload"
      :action="uploadUrl"
      @change="handleChangeFile"
>

  

       beforeUpload(file) {
            let reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = () => {
                const image = new Image();
                image.src = reader.result;
                image.onload = () => {
                    this.imgWidth = image.width;
                    this.imgHeight = image.height;
                    console.log(this.imgWidth, this.imgHeight); //文件像素大小
    
                }
            }
            const isJpgOrPng =
                file.type === "image/jpeg" || file.type === "image/png";
            if (!isJpgOrPng) {
                this.$message.error("僅可上傳jpg和png文件");
            }
            const isLt2M = file.size / 1024 / 1024 < 10;
            if (!isLt2M) {
                this.$message.error("圖片文件要求小於10M");
            }
            return isJpgOrPng && isLt2M;
        }    

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM