图片转base64【Vue + 纯Html】

1.template

<el-form-item label="图片">
   <div class="image-upload-container">
      <input type="file" id="imageUpload" class="image-upload" @change="convertToBase64" />
      <label for="imageUpload" class="image-upload-label">选择图片</label>
      <div class="image-preview">
       <img v-if="form.ilImgPath" :src="form.ilImgPath" alt="预览图片" class="preview-image" />
      </div>
    </div>
</el-form-item>

2.methods

    /**
     * 转base64
     * @param {*} event 图片文件选中信息
     */
    convertToBase64(event) {
      const file = event.target.files[0];
      console.log(file);
      if (file && file.size / 1024 / 1024 < 0.9) {
        const reader = new FileReader();
        reader.onload = (e) => {
          this.form.ilImgPath = e.target.result;
        };
        reader.readAsDataURL(file);
      } else {
        this.$message.error("请正确选择图片!或选择大小小于900KB的图片");
      }
    },

3.style

<style>
.image-upload-container {  
  display: flex;  
  flex-direction: column;  
  align-items: center;  
  margin-bottom: 20px; /* 根据需要调整 */  
}  
  
.image-upload {  
  display: none; /* 隐藏原生的文件选择框 */  
  cursor: pointer; /* 仍然保持可点击的视觉效果 */  
}  
  
.image-upload-label {  
  display: inline-block;  
  padding: 8px 16px;  
  background-color: #f2f2f2;  
  border: 1px solid #ccc;  
  border-radius: 4px;  
  cursor: pointer;  
  transition: background-color 0.3s ease;  
}  
  
.image-upload-label:hover {  
  background-color: #e6e6e6;  
}  
  
.image-preview {  
  margin-top: 10px; /* 根据需要调整 */  
  text-align: center; /* 图片居中显示 */  
}  
  
.preview-image {  
  max-width: 100%; /* 图片最大宽度为容器宽度 */  
  height: auto; /* 图片高度自动以适应宽度 */  
  border-radius: 4px; /* 圆角边框 */  
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 阴影效果 */  
}
</style>

示例:在这里插入图片描述

上传文件转base64.html【免费】

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程似锦吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值