当前位置:首页 > VUE

vue实现文件上传前端

2026-02-25 06:17:39VUE

使用 Vue 实现文件上传前端

安装依赖 在项目中安装 axios 用于发送文件上传请求:

npm install axios

创建文件上传组件 创建一个 Vue 组件,包含文件选择输入和上传按钮:

vue实现文件上传前端

<template>
  <div>
    <input type="file" @change="handleFileChange" />
    <button @click="uploadFile">上传文件</button>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      file: null
    };
  },
  methods: {
    handleFileChange(event) {
      this.file = event.target.files[0];
    },
    uploadFile() {
      if (!this.file) {
        alert('请选择文件');
        return;
      }

      const formData = new FormData();
      formData.append('file', this.file);

      axios.post('/api/upload', formData, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then(response => {
        alert('上传成功');
      }).catch(error => {
        alert('上传失败');
      });
    }
  }
};
</script>

处理多文件上传 如果需要支持多文件上传,修改 handleFileChange 方法:

handleFileChange(event) {
  this.files = event.target.files;
},
uploadFiles() {
  const formData = new FormData();
  Array.from(this.files).forEach(file => {
    formData.append('files', file);
  });

  axios.post('/api/upload-multiple', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

显示上传进度 添加进度条显示上传进度:

vue实现文件上传前端

<template>
  <div>
    <input type="file" @change="handleFileChange" />
    <button @click="uploadFile">上传文件</button>
    <progress :value="progress" max="100"></progress>
  </div>
</template>

<script>
export default {
  data() {
    return {
      progress: 0
    };
  },
  methods: {
    uploadFile() {
      const config = {
        headers: {
          'Content-Type': 'multipart/form-data'
        },
        onUploadProgress: progressEvent => {
          this.progress = Math.round(
            (progressEvent.loaded * 100) / progressEvent.total
          );
        }
      };

      axios.post('/api/upload', formData, config)
    }
  }
};
</script>

文件类型和大小验证 在上传前验证文件类型和大小:

uploadFile() {
  const allowedTypes = ['image/jpeg', 'image/png'];
  const maxSize = 2 * 1024 * 1024; // 2MB

  if (!allowedTypes.includes(this.file.type)) {
    alert('仅支持JPEG和PNG格式');
    return;
  }

  if (this.file.size > maxSize) {
    alert('文件大小不能超过2MB');
    return;
  }

  // 上传逻辑
}

使用第三方库 考虑使用 vue-upload-component 等第三方库简化实现:

npm install vue-upload-component
<template>
  <file-upload
    url="/api/upload"
    @input-file="inputFile"
    @input-filter="inputFilter"
  ></file-upload>
</template>

<script>
import FileUpload from 'vue-upload-component';

export default {
  components: {
    FileUpload
  },
  methods: {
    inputFile(newFile, oldFile) {
      // 文件选择回调
    },
    inputFilter(newFile, oldFile, prevent) {
      // 文件过滤
    }
  }
};
</script>

标签: 文件上传vue
分享给朋友:

相关文章

vue实现分屏

vue实现分屏

Vue 实现分屏的方法 分屏功能通常用于展示多个独立内容区域,以下是几种常见的实现方式: 使用 CSS Grid 布局 通过 CSS Grid 可以轻松创建分屏布局,在 Vue 组件中直接应用样式…

vue 方法实现

vue 方法实现

在 Vue 中,方法的实现通常通过 methods 选项完成。以下是 Vue 方法实现的核心要点和示例: 基本方法定义 在 Vue 组件中定义方法时,需将函数声明放在 methods 对象内。这些方…

vue 实现过滤

vue 实现过滤

Vue 实现过滤的方法 在 Vue 中实现过滤功能可以通过多种方式完成,包括使用计算属性、自定义过滤器、第三方库等。以下是几种常见的实现方法: 使用计算属性进行过滤 计算属性是 Vue 中最常用的过…

vue实现popup

vue实现popup

Vue 实现 Popup 弹窗 使用 Vue 原生组件 创建一个基本的 Vue 组件作为弹窗,通过 v-if 或 v-show 控制显示隐藏。 <template> <div&…

tabbar 实现 vue

tabbar 实现 vue

在 Vue 中实现 TabBar 功能可以通过多种方式完成,以下是几种常见的方法: 使用 Vue Router 结合自定义组件 创建一个自定义的 TabBar 组件,结合 Vue Router 实…

vue实现视频

vue实现视频

Vue 实现视频播放的方法 在 Vue 项目中实现视频播放可以通过多种方式完成,以下是几种常见的方法: 使用 HTML5 的 <video> 标签 在 Vue 组件中直接使用 HT…