咏竹莉
where there is a will,there is a way
posts - 132,comments - 1,views - 66816
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<el-upload
           class="upload-demo"
           :action="uploadLi"
            :before-upload="beforeUpload"
            :on-success="handleSuccess"
            :headers="{'X-User-Token': authorization}"
             name="file"
             accept="image/png,image/jpg,image/jpeg,image/gif"
             :multiple=false
             :show-file-list=false
              >
              <div class="avatar" v-if="createForm.advertPic">
                   <img :src="createForm.advertPic" class="avatar_img">
                    <span>修改图标</span>
              </div>
             <el-button size="small" type="primary" style="height:28px;" v-else>上传</el-button>
              <div slot="tip" class="el-upload__tip" style="margin-top:0px;">只能上传jpg/png/gif文件 750*1642px,1M以内</div>
 </el-upload>

 

js: 

复制代码
handleAvatarSuccess(res, file) {
  console.log(res)
  console.log(file)
  this.imageUrl = URL.createObjectURL(file.raw);
  //
  if (res.code !== 200) {
    this.$message.error('上传失败!')
  } else {
    this.isShowRequest = false
    this.isLoad = false
    this.modalForm.logoId = res.body.fileId
    this.modalForm.logoPath = res.body.filePath
  }
},
beforeAvatarUpload(file) {
  console.log(file)
  const isJpgPng = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif'

  if (!isJpgPng) {
    // this.$message.error('上传文件格式只能是jpg/png')
    this.isLoad = true
    this.errText = '上传文件格式只能是jpg/png/gif'
    // let _t = this
    // setTimeout(function () {
    //   _t.isLoad = false
    // },3000)
    return false
  }

  const isLt1M = file.size / 1024 / 1024 < 1
  if (!isLt1M) {
    // this.$message.error('上传图片大小不能超过 1MB!')
    this.isLoad = true
    this.errText = '上传图片大小不能超过 1MB!'
    // let _t = this
    // setTimeout(function(){
    //   _t.isLoad = false
    // },3000)
    return false
  }

  const isSize = new Promise(function(resolve, reject){
    // let width = 750;
    // let height = 1642;
    // let _URL = window.URL || window.webkitURL;
    let img = new Image();
    img.onload = function(){
      let valid = img.width / img.height === 1.67;
      valid ? resolve() : reject();
    }
    img.src = _URL.createObjectURL(file);
  }).then(()=>{
    return file;
  }, ()=>{
    // this.$message({
    //   message:'上传图片比例建议为5:3!请重新选择!'
    // });
    this.isLoad = true
    this.errText = '上传图片比例只能为5:3, 请重新选择上传!'
    return Promise.reject()
    return false;//必须加上return false; 才能阻止
  })

  return isJpgPng && isLt1M && isSize
}
  
复制代码

 

posted on   咏竹莉  阅读(2323)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示