vue3上传文件,图片xsl,word,.py,文件的上传

我用的是element-plus的上传插件

template,部分

                  <el-upload
                     class="avatar-uploader"
                     action="/api/admin/product/fileUpload"  //这是我们要上传的请求地址
                     :show-file-list="false"  如果是true我们上传的文件显不显示
                     :on-success="handleAvatarSuccess"  上传成功后的回调..但是无比看官网,因为这个函数有ts类型,如果不拿过来,肯能会报错
                     :before-upload="beforeAvatarUpload" //上传之前的回调.有一个参数
                  >
            <img v-if="TradeMarkForm.logoUrl" :src="TradeMarkForm.logoUrl" //我们选中的图片
 



class="avatar" />
                     <el-icon v-else class="avatar-uploader-icon"><UploadFilled /></el-icon>
                  </el-upload> //图标

sicript部分

import type { UploadProps } from 'element-plus' //这是elment-plus提供的ts类型


// 上传图片前的准备
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile:any) => {
       if(rawFile.type=="image/png" || rawFile.type=="image/jpeg" || rawFile.type=="image/gif" ){
               if(rawFile.size/1024/1024<6){
                   return true
               }else{
                   ElMessage({
                        type:"error",
                        message:"图片过于庞大"
                   })
                   return false;
               }
       }else{
            
         ElMessage({
             type:'error',
             message:"图片必须是JPEG,PNG,GIF类型的"
         })
         return false
       }
}
// 图片上传成功的回调
const handleAvatarSuccess: UploadProps['onSuccess'] = (response:any,uploadFile:any) => {

      TradeMarkForm.logoUrl = response.data
}

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值