vue限制文件上傳大小和上傳格式


<el-form-item label="圖片:" prop="tempImagePath">
  <el-upload
    class="upload"
    accept="image/jpeg"
    :show-file-list="false"
    list-type="picture-card"
    :headers="{ token: token}"
    :action="actionUrl"
    :before-upload="beforeAvatarUpload"
    :on-success="handleAvatarSuccess">
    <img v-if="temp.tempImagePath"
         :src="temp.tempImagePath" width="146px" height="146px"/>
    <i v-else class="el-icon-plus"></i>
    <div slot="tip" class="el-upload__tip">
      只能上傳.gif/.jpeg/.png文件且小於500K
    </div>
  </el-upload>
</el-form-item>

 





beforeAvatarUpload (file) {
  const imgType = file.type === 'image/jpeg' || file.type === 'image/png'
  const isLt500k = file.size / 1024 / 1024 < 0.48;
  if (!imgType) {
    this.$message.error("上傳圖片只能是 JPG和png 格式!");
    return false;
  }
  if (!isLt500k) {
    this.$message.error("上傳圖片大小不能超過 500k!");
    return false;
  }
},

 


免責聲明!

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



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