当前位置:首页 > VUE

vue实现附件组件

2026-01-17 17:38:32VUE

Vue 实现附件组件的方法

基础组件结构

使用 Vue 的单文件组件(SFC)方式创建一个基础的附件上传组件。需要包含文件选择、预览、上传和删除功能。

<template>
  <div class="attachment-component">
    <input type="file" @change="handleFileChange" multiple />
    <div v-for="(file, index) in files" :key="index" class="file-item">
      <span>{{ file.name }}</span>
      <button @click="removeFile(index)">删除</button>
    </div>
    <button @click="uploadFiles">上传</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      files: []
    }
  },
  methods: {
    handleFileChange(event) {
      this.files = Array.from(event.target.files)
    },
    removeFile(index) {
      this.files.splice(index, 1)
    },
    uploadFiles() {
      // 上传逻辑
    }
  }
}
</script>

文件预览功能

对于图片类型的附件,可以添加预览功能。使用 URL.createObjectURL 生成临时链接显示预览。

<template>
  <div v-for="(file, index) in files" :key="index">
    <img v-if="file.type.startsWith('image/')" :src="getPreview(file)" width="100" />
    <span>{{ file.name }}</span>
  </div>
</template>

<script>
methods: {
  getPreview(file) {
    return URL.createObjectURL(file)
  },
  beforeDestroy() {
    this.files.forEach(file => {
      URL.revokeObjectURL(this.getPreview(file))
    })
  }
}
</script>

上传功能实现

使用 axios 或其他 HTTP 客户端实现文件上传功能。需要构建 FormData 对象发送文件数据。

methods: {
  async uploadFiles() {
    const formData = new FormData()
    this.files.forEach(file => {
      formData.append('files', file)
    })

    try {
      const response = await axios.post('/api/upload', formData, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
      console.log('上传成功', response.data)
    } catch (error) {
      console.error('上传失败', error)
    }
  }
}

进度显示

添加上传进度显示功能,通过 axios 的 onUploadProgress 回调更新进度条。

<template>
  <div v-if="uploading">
    上传进度: {{ progress }}%
    <progress :value="progress" max="100"></progress>
  </div>
</template>

<script>
data() {
  return {
    uploading: false,
    progress: 0
  }
},
methods: {
  async uploadFiles() {
    this.uploading = true
    try {
      await axios.post('/api/upload', formData, {
        onUploadProgress: progressEvent => {
          this.progress = Math.round(
            (progressEvent.loaded * 100) / progressEvent.total
          )
        }
      })
    } finally {
      this.uploading = false
    }
  }
}
</script>

文件限制

添加文件类型和大小限制,在上传前进行验证。

methods: {
  handleFileChange(event) {
    const files = Array.from(event.target.files)
    const validFiles = files.filter(file => {
      const isValidType = ['image/jpeg', 'image/png'].includes(file.type)
      const isValidSize = file.size < 2 * 1024 * 1024 // 2MB
      return isValidType && isValidSize
    })
    this.files = validFiles
  }
}

拖放上传

实现拖放文件上传功能,提升用户体验。

<template>
  <div 
    class="drop-zone"
    @dragover.prevent="dragover"
    @dragleave.prevent="dragleave"
    @drop.prevent="drop"
    :class="{ 'drag-active': isDragActive }"
  >
    拖放文件到这里
  </div>
</template>

<script>
data() {
  return {
    isDragActive: false
  }
},
methods: {
  dragover() {
    this.isDragActive = true
  },
  dragleave() {
    this.isDragActive = false
  },
  drop(event) {
    this.isDragActive = false
    this.handleFileChange(event)
  }
}
</script>

组件封装

将组件封装为可复用的形式,通过 props 接收配置参数,通过 emits 触发事件。

vue实现附件组件

<script>
export default {
  props: {
    maxSize: {
      type: Number,
      default: 2 * 1024 * 1024
    },
    allowedTypes: {
      type: Array,
      default: () => ['image/jpeg', 'image/png']
    }
  },
  emits: ['upload-success', 'upload-error'],
  methods: {
    async uploadFiles() {
      try {
        const response = await axios.post('/api/upload', formData)
        this.$emit('upload-success', response.data)
      } catch (error) {
        this.$emit('upload-error', error)
      }
    }
  }
}
</script>

标签: 组件附件
分享给朋友:

相关文章

vue实现组件封装

vue实现组件封装

Vue 组件封装方法 封装组件的基本步骤 创建独立的 .vue 文件,包含 template、script 和 style 三个部分。通过 props 接收父组件传递的数据,使用 $emit 触发自…

vue 实现组件刷新

vue 实现组件刷新

实现组件刷新的方法 在Vue中,组件刷新通常指重新渲染组件或重置组件状态。以下是几种常见的实现方式: 使用v-if控制渲染 通过v-if指令可以销毁并重新创建组件: <template>…

vue实现下拉框组件

vue实现下拉框组件

Vue 下拉框组件实现 基础实现 创建基础下拉框组件,使用v-model实现双向数据绑定: <template> <div class="dropdown"> &…

vue VUE实现日历组件功能

vue VUE实现日历组件功能

Vue 日历组件实现方法 基础日历结构 使用Vue的模板语法构建日历的HTML结构,通常包含头部(年月显示、切换按钮)和日历表格(星期标题和日期格子)。 <template> <…

vue实现盖章组件

vue实现盖章组件

实现Vue盖章组件的步骤 组件结构设计 创建一个Vue组件,包含画布元素和必要的控制按钮。画布用于显示盖章效果,按钮用于触发盖章操作。组件需要接收图片和盖章位置等参数。 <template&g…

vue实现弹窗组件

vue实现弹窗组件

实现弹窗组件的基本结构 在Vue中创建一个弹窗组件通常需要三个核心部分:组件模板、样式和逻辑控制。弹窗组件应具备打开、关闭功能,并支持内容插槽或属性传入。 <template> &l…