前端OSS分片上传(vue版)

使用vue前端大文件直传OSS、使用分片上传

<template>
  <div class="big-file-upload">
    <div class="el-upload el-upload--text">
      <div class="el-upload-dragger" :class="isDrag?'is-dragover':''" @click.stop="onUploadFile">
        <div class="el-upload__text">
          <i class="el-icon-upload" />将文件拖到此处,或<em>点击选择文件</em>
        </div>
        <input ref="file" type="file" name="file" multiple="multiple" class="el-upload__input" @change="onFileChange">
      </div>
    </div>
    <!-- 文件上传table -->
    <vxe-grid
      ref="bigFileUploadTable"
      class="big-file-upload-tab"
      :data="fileList"
      size="small"
      border="inner"
      show-overflow="title"
      highlight-hover-row
      highlight-current-row
    >
      <vxe-table-column align="center" type="seq" width="30" fixed="left" />
      <vxe-table-column :show-overflow="false" field="name" title="文件名" align="left" width="200">
        <template slot-scope="scope">
          <div v-if="scope.row.isEditFileName">
            <el-input v-model="scope.row.fileName" size="mini">
              <el-button slot="append" icon="el-icon-finished" @click.stop="onChangeFileName(scope)" />
            </el-input>
          </div>
          <div v-else>
            <a :title="scope.row.fileName" class="file-name" @click.stop="onPreviewFile(scope.row)">{
  { scope.row.fileName }}</a>
            <i style="display: inline-block;vertical-align: 5px;cursor: pointer;font-size: 15px;color: #0099ff;" class="el-icon-edit" @click.stop="onChangeFileName(scope)" />
          </div>
        </template>
      </vxe-table-column>
      <vxe-table-column field="size" title="大小" width="90" align="center" />
      <vxe-table-column field="type" title="类型" width="100" align="center" />
      <vxe-table-column field="durationStr" title="时长" width="80" align="center" />
      <vxe-table-column field="progress" title="上传进度" width="180" align="center">
        <template slot-scope="scope">
          <el-progress :text-inside="true" :stroke-width="15
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue OSS 分片上传是指将大文件分成多个小块,分别上传到阿里云 OSS(Object Storage Service)中,最后再将这些小块合并成一个完整的文件。这样做的好处是可以避免上传大文件时出现各种问题,比如上传速度慢、网络中断等等。 下面是一个基于 Vue 和阿里云 OSS分片上传示例: 1. 安装阿里云 OSS SDK 和 axios: ```javascript npm install ali-oss axios --save ``` 2. 创建一个上传组件 Upload.vue: ```vue <template> <div> <input type="file" ref="fileInput" @change="selectFile" /> <button @click="uploadFile">上传</button> </div> </template> <script> import OSS from 'ali-oss' import axios from 'axios' export default { data() { return { selectedFile: null, ossClient: null, uploadId: null, chunks: [], uploadedChunks: [] } }, methods: { selectFile(event) { this.selectedFile = event.target.files[0] }, async uploadFile() { // 初始化 OSS 客户端 this.ossClient = new OSS({ accessKeyId: 'your_access_key_id', accessKeySecret: 'your_access_key_secret', bucket: 'your_bucket_name', region: 'your_region' }) // 获取文件信息 const fileInfo = await this.ossClient.head(this.selectedFile.name) // 计算文件分片数 const chunkSize = 1 * 1024 * 1024 // 每个分片大小为 1MB const chunkCount = Math.ceil(fileInfo.res.headers['content-length'] / chunkSize) // 初始化分片上传 const result = await this.ossClient.initMultipartUpload(this.selectedFile.name) this.uploadId = result.uploadId // 分片上传 for (let i = 0; i < chunkCount; i++) { const start = i * chunkSize const end = Math.min(start + chunkSize, fileInfo.res.headers['content-length']) const chunk = this.selectedFile.slice(start, end) this.chunks.push(chunk) } await Promise.all( this.chunks.map(async (chunk, index) => { const result = await this.ossClient.uploadPart(this.selectedFile.name, this.uploadId, index + 1, chunk) this.uploadedChunks.push({ PartNumber: index + 1, ETag: result.etag }) }) ) // 完成分片上传 await this.ossClient.completeMultipartUpload(this.selectedFile.name, this.uploadId, this.uploadedChunks) // 上传完成,发送请求到服务器 const response = await axios.post('/api/upload', { filename: this.selectedFile.name }) console.log(response.data) } } } </script> ``` 3. 在服务器端(假设使用 Express)处理上传请求: ```javascript const express = require('express') const app = express() app.post('/api/upload', (req, res) => { const filename = req.body.filename // 处理上传完成后的逻辑 }) app.listen(3000, () => { console.log('Server is running at http://localhost:3000') }) ``` 这样就完成了基于 Vue 和阿里云 OSS分片上传示例。需要注意的是,阿里云 OSS分片上传 API 是收费的,具体收费标准可以参考阿里云官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值