当前位置:首页 > JavaScript

JS实现头像上传预览

2026-01-31 03:29:19JavaScript

实现头像上传预览的方法

HTML部分 创建一个文件输入框和一个用于预览的图片元素:

<input type="file" id="avatar-upload" accept="image/*">
<img id="avatar-preview" src="#" alt="头像预览" style="display:none; max-width:200px;">

JavaScript部分 监听文件输入变化并处理预览:

document.getElementById('avatar-upload').addEventListener('change', function(e) {
  const file = e.target.files[0];
  if (!file) return;

  const preview = document.getElementById('avatar-preview');
  const reader = new FileReader();

  reader.onload = function(e) {
    preview.src = e.target.result;
    preview.style.display = 'block';
  }

  reader.readAsDataURL(file);
});

添加图片验证功能

在读取文件前检查文件类型和大小:

const MAX_SIZE = 2 * 1024 * 1024; // 2MB
if (!file.type.match('image.*')) {
  alert('请选择图片文件');
  return;
}

if (file.size > MAX_SIZE) {
  alert('图片大小不能超过2MB');
  return;
}

使用canvas压缩图片

对大图片进行压缩处理:

function compressImage(file, callback) {
  const img = new Image();
  const canvas = document.createElement('canvas');
  const ctx = canvas.getContext('2d');

  img.onload = function() {
    const MAX_WIDTH = 800;
    const MAX_HEIGHT = 800;
    let width = img.width;
    let height = img.height;

    if (width > height) {
      if (width > MAX_WIDTH) {
        height *= MAX_WIDTH / width;
        width = MAX_WIDTH;
      }
    } else {
      if (height > MAX_HEIGHT) {
        width *= MAX_HEIGHT / height;
        height = MAX_HEIGHT;
      }
    }

    canvas.width = width;
    canvas.height = height;
    ctx.drawImage(img, 0, 0, width, height);
    callback(canvas.toDataURL('image/jpeg', 0.7));
  };

  img.src = URL.createObjectURL(file);
}

完整实现示例

document.getElementById('avatar-upload').addEventListener('change', function(e) {
  const file = e.target.files[0];
  if (!file) return;

  const MAX_SIZE = 2 * 1024 * 1024;
  if (!file.type.match('image.*')) {
    alert('请选择图片文件');
    return;
  }

  if (file.size > MAX_SIZE) {
    compressImage(file, function(compressedDataUrl) {
      const preview = document.getElementById('avatar-preview');
      preview.src = compressedDataUrl;
      preview.style.display = 'block';
    });
  } else {
    const preview = document.getElementById('avatar-preview');
    const reader = new FileReader();
    reader.onload = function(e) {
      preview.src = e.target.result;
      preview.style.display = 'block';
    }
    reader.readAsDataURL(file);
  }
});

样式优化建议

为上传区域添加更好的视觉效果:

#avatar-preview {
  border-radius: 50%;
  object-fit: cover;
  width: 120px;
  height: 120px;
  margin-top: 10px;
  border: 2px solid #eee;
}

#avatar-upload {
  display: none;
}

.upload-label {
  display: inline-block;
  padding: 8px 15px;
  background: #4CAF50;
  color: white;
  cursor: pointer;
  border-radius: 4px;
}

对应的HTML调整:

<label for="avatar-upload" class="upload-label">选择头像</label>
<input type="file" id="avatar-upload" accept="image/*">

JS实现头像上传预览

标签: 头像上传
分享给朋友:

相关文章

vue实现上传

vue实现上传

Vue 实现文件上传 在 Vue 中实现文件上传功能可以通过原生 HTML5 的 <input type="file"> 元素结合 Vue 的数据绑定和事件处理来实现。以下是几种常见的实现…

vue 实现上传

vue 实现上传

Vue 实现文件上传的方法 在 Vue 中实现文件上传可以通过多种方式完成,以下是几种常见的方法: 使用原生 HTML 的 input 元素 通过 HTML 的 input 元素设置 type="f…

vue实现附件上传

vue实现附件上传

文件上传的基本实现 在Vue中实现文件上传可以通过原生HTML的<input type="file">元素结合Vue的数据绑定和事件处理来完成。创建一个文件上传组件,监听change事件获…

vue实现多图片上传

vue实现多图片上传

Vue 多图片上传实现 基本文件上传组件 创建基于<input type="file">的基础组件,支持多选: <template> <div> &l…

vue实现拍摄视频上传

vue实现拍摄视频上传

Vue实现拍摄视频上传的方法 使用HTML5的<input type="file">和capture属性 在Vue模板中添加一个文件输入元素,通过capture属性指定使用摄像头拍摄视频:…

react实现照片上传

react实现照片上传

实现照片上传的基本步骤 在React中实现照片上传功能通常涉及前端表单处理、文件选择、预览以及后端API交互。以下是具体实现方法: 创建文件上传表单组件 使用HTML的<input type…