template内容:
<a-upload-dragger name="file" :multiple="false" :beforeUpload="addVideos">
<p class="ant-upload-drag-icon">
<inbox-outlined />
</p>
<p class="ant-upload-text">上传视频</p>
</a-upload-dragger>
script ts 内容
<script setup lang="ts">
import videoApi from '/@/api/video'; *下面有说明*
const addVideos = async (e) => {
const file = e;
if (file == null || file.length == 0) return;
// 切片
let files = createFileChunk(file, 5 * 1024 * 1024);
const chunkCount = files.length;
// 切片上传
for (let index = 0; index < chunkCount; index++) {
const videoData = new FormData();
videoData.append('file', files[index].file);
videoData.append('current', index.toString());
videoData.append('filename', file.name);
let res = await videoApi.uploadVideo(videoData);
if (res || res.code == 200) {
if (index == chunkCount - 1) {
const mergeData = new FormData();
mergeData.append('chunks', chunkCount);
mergeData.append('filename', file.name);
let mergeRes = await videoApi.mergeVideo(mergeData);
}
} else {
break;
}
}
return false;
};
const createFileChunk = (file, size) => {
const fileChunkList: any = [];
var count = 0;
while (count < file.size) {
fileChunkList.push({
file: file.slice(count, count + size),
});
count += size;
}
return fileChunkList;
};
</script>
引用的 /api/video内容是:
下载方法见下一篇(也用到这个地方)
import { ReqParams, ResResult } from './model';
import { deleted, post } from '/@/utils/http';
enum API {
/**
* 视频切片
*/
uploadVideoChunk = '/common/upload/chunk',
/**
* 合并视频
*/
uploadMerginVideo = '/common/upload/merge',
/**
* 剪辑并下载视频
*/
videoDown = '/manage/video/download',
}
/**
* 采用分片上传每个视频
*/
const uploadVideo = async (data) => {
const headers = { 'Content-Type': 'application/octet-stream' };
return post({ headers: headers, url: API.uploadVideoChunk, data });
};
const mergeVideo = async (data) => {
return post({ url: API.uploadMerginVideo, data });
};
const videoDown = async (data) => {
return post<ResResult>({ url: API.videoDown, data, responseType: 'blob' });
};
export default {
uploadVideo,
mergeVideo,
videoDown,
};