vue3 文件资源上传阿里云,包含大文件的切片上传和断点续传

本文介绍如何在Vue3应用中实现阿里云的文件资源上传功能,特别是针对大文件进行切片上传和断点续传的详细步骤和示例。
摘要由CSDN通过智能技术生成

文件资源上传阿里云,包含大文件的切片上传和断点续传

vue3 upload

属性说明

属性名类型默认值说明
optionsObject{}属性

事件

事件名类型默认值说明
@uploadSucessFunction() => {}文件上传成功
@uploadProcessFunction() => {}文件上传过程
@beforeUploadFunction() => {}文件上传前
@uploadErrorFunction() => {}文件上传失败

使用示例

<upload-file
    ref="uploadFileDom"
    :options="{
      multiple: true,
      accept,
      isAddSucaiFlag: true,
      uploadSize: '2gb',
      testNameLen: 90
    }"
    @uploadSucess="uploadSucess"
    @uploadProcess="uploadProcess"
    @beforeUpload="beforeUpload"
    @uploadError="uploadError"
  ></upload-file>
  <el-button @click='hanldeUpload'></el-button>
// setup 形式下
import { ref,reactive,shallowRef } from 'vue'
import uploadFile from "@/components/upload-v3/upload"
const uploadFileDom = ref("")
import { getAccept, strLength } from "@/components/upload-v3/index";
const accept = shallowRef(getAccept(2))

const beforeUpload = (fileList) => {
		uploadList.length = 0
		uploadList.push(...fileList)
	}
	const uploadProcess = (fileList, item) => {
		uploadList.forEach(ele => {
			if(ele.id === item.id) {
				ele.process = item.process
				ele.uploadId = item.uploadId
			}
		})
	}
	const uploadSucess = async (fileList, item, tag) => {
		if(searchRuleForm) {
			searchRuleForm.tag = tag
		}
		const index = uploadList.findIndex(ele => ele.id === item.id)
		uploadList.splice(index, 1)
		delete item.process
		tableData.unshift(item)
		if(fn) {
			fn(item)
		}
	}
	const uploadError = (item) => {
		uploadList.forEach(ele => {
			if(ele.id === item.id) {
				ele.type = 'error'
			}
		})
	}
	const cancelUpload = (row) => {
		if(row) {
			uploadFileDom.value.cancelUpload(row.id)
		} else {
			uploadFileDom.value.cancelUpload()
		}
	}
	const reloadUpload = (row) => {
		uploadFileDom.value.reloadFile(row)
	}
	const deleteUpload = (row) => {
		const index = uploadList.findIndex(ele => ele.id === row.id)
		uploadList.splice(index, 1)
		uploadFileDom.value.deleteUpload(row.id)
	}
	const hanldeUpload = () => {
		uploadFileDom.value.uploadFile()
	}
	const intercept = function (callback) {
		if(uploadList.length) {
			ElMessageBox.confirm('当前还有文件正在上传,当前操作会取消上传', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				callback: (action) => {
					if(action === 'confirm') {
						cancelUpload()
						uploadFileDom.value.deleteUpload()
						callback()
					}
				}
			})
		} else {
			callback()
		}
	}


//也可以注册到全局 页面中直接使用不需要import
// main.ts文件
import App from '@/App.vue'
import uploadFile from "@/components/upload-v3/upload"
const app = createApp(App)
app.component('upload-file', uploadFile)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中实现大文件上传功能,断点续传和拆分上传,你可以按照以下步骤进行操作: 1. 使用`<input type="file">`元素让用户选择文件。 2. 在Vue组件中,使用FormData对象来构建上传请求,并将文件块作为数据项添加到FormData中。 3. 将文件块通过AJAX或axios等网络库发送到服务器。在发送请求时,你可以设置请求头中的`Content-Range`字段来指定上传文件块的范围。 4. 服务器端接收到文件块后,将其保存到临时目录中,并记录已上传文件块信息,如文件名、文件大小、文件块编号等。 5. 当用户选择继续上传时,前端可以通过发送一个特殊请求到服务器,获取已上传文件块信息。 6. 前端根据服务器返回的已上传文件块信息,判断哪些文件块还未上传,然后继续从未上传文件块开始上传。 7. 重复步骤3和步骤4,直到所有文件块都上传完毕。 8. 服务器端接收到所有文件块后,将它们合并成完整的文件。 9. 删除临时保存的文件块和记录。 需要注意的是,以上只是实现大文件上传功能的基本步骤。在实际开发中,你还需要处理错误情况、显示上传进度、添加用户交互等。你可以使用一些现有的Vue组件库,如vue-file-agent、vue-upload-component等来简化开发过程。同时,服务器端的实现也需要相应的处理,以确保文件块的正确接收、合并和存储。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值