JavaScript+PHP实现视频文件分片上传

news/2024/9/8 11:04:19

摘要

视频文件分片上传,整体思路是利用JavaScript将文件切片,然后循环调用上传接口 upload.php 将切片上传到服务器。这样将由原来的一个大文件上传变为多个小文件同时上传,节省了上传时间,这就是文件分片上传的其中一个好处。

在这里插入图片描述

上代码

index.html

通过前端将文件对象切分成多个小块,然后依次将这些小块的文件对象上传到服务器。

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>视频文件分片上传</title><style>*{padding: 0;margin: 0;}.title {text-align: center;font-size: 25px;margin-top: 50px;}.video_upload {width: 500px;height: 60px;background: #eee;margin: 30px auto 0;border: 2px dashed #ccc;border-radius: 10px;position: relative;cursor: pointer;text-align: center;font-size: 25px;line-height: 60px;color: #666;}#fileInput {width: 100%;height: 100%;position: absolute;left: 0;top: 0;opacity: 0;cursor: pointer;}#uploadButton {width: 130px;height: 40px;border: none;outline: none;border-radius: 10px;font-size: 17px;margin: 10px auto;}#ret {text-align: center;font-size: 16px;margin-top: 20px;}#ret video {width: 450px;}</style></head><body><p class="title">javaScript+PHP实现视频文件分片上传</p><div class="video_upload"><span class="text"> + </span><input type="file" id="fileInput" accept="video/*"></div><button id="uploadButton" style="display:none;">开始上传</button><p id="ret"></p><script>// 定义全局变量let videoFile = null;let chunkSize = 1024 * 1024; // 1MB 分片大小// 当文件选择框的值改变时触发该函数function handleFileSelect(event) {const fileList = event.target.files;if (fileList.length > 0) {videoFile = fileList[0];console.log("选择了文件: ", videoFile.name);document.querySelector('.video_upload .text').textContent = videoFile.name;document.querySelector('#uploadButton').style.display = 'block';}}// 分片并上传文件async function uploadFile() {if (!videoFile) {console.error("请选择一个视频文件");return;}const fileSize = videoFile.size;let start = 0;let end = Math.min(chunkSize, fileSize);let chunkIndex = 0;// 获取文件名const fileName = videoFile.name;while (start < fileSize) {const chunk = videoFile.slice(start, end); // 从文件中截取一个分片// 使用FormData来构建multipart/form-data格式的请求体const formData = new FormData();formData.append('file', chunk);formData.append('chunkIndex', chunkIndex);formData.append('fileName', fileName); // 将文件名作为 formData 的一部分try {const response = await fetch('upload.php', {method: 'POST',body: formData});if (!response.ok) {throw new Error('上传失败');}console.log('上传分片 ', chunkIndex, ' 成功');} catch (error) {console.error('上传分片 ', chunkIndex, ' 失败: ', error.message);return;}start = end;end = Math.min(start + chunkSize, fileSize);chunkIndex++;}console.log('文件上传完成');// 上传完成后发送通知给服务器进行合并notifyServerForMerge(fileName);}// 发送通知给服务器进行合并async function notifyServerForMerge(fileName) {try {const response = await fetch('merge_chunks.php', {method: 'POST',headers: {'Content-Type': 'application/json'},body: JSON.stringify({ fileName: fileName })});if (!response.ok) {throw new Error('无法通知服务器进行合并');}const res_data = await response.json();console.log('已通知服务器进行合并');document.querySelector('.video_upload .text').textContent = '分片合并完成!';document.querySelector('#ret').innerHTML = '<video autoplay controls src="'+res_data.filePath+'"></video>';document.querySelector('#uploadButton').style.display = 'none';} catch (error) {console.error('通知服务器进行合并时发生错误: ', error.message);}}// 注册文件选择框的change事件document.getElementById('fileInput').addEventListener('change', handleFileSelect);// 注册上传按钮的click事件document.getElementById('uploadButton').addEventListener('click', uploadFile);</script></body>
</html>

upload.php

这个是用于接收前端传过来的每一段分片,然后上传到 uploads 文件夹,上传之后就是一段一段的小分片。

<?php// 设置允许跨域访问header("Access-Control-Allow-Origin: *");header("Access-Control-Allow-Methods: POST");// 检查是否接收到文件和分片索引if (isset($_FILES['file']['error']) && isset($_POST['chunkIndex']) && isset($_POST['fileName'])) {$error = $_FILES['file']['error'];$chunkIndex = $_POST['chunkIndex'];$fileName = $_POST['fileName']; // 获取文件名// 检查是否有错误if ($error !== UPLOAD_ERR_OK) {http_response_code(500);echo json_encode(array('error' => '文件上传失败'));exit();}// 设置存储目录和文件名$uploadDir = './uploads/';$filePath = $uploadDir . $fileName . '.' . $chunkIndex;// 将分片移动到指定的目录if (move_uploaded_file($_FILES['file']['tmp_name'], $filePath)) {echo json_encode(array('success' => '分片上传成功'));} else {http_response_code(500);echo json_encode(array('error' => '分片上传失败'));}} else {http_response_code(400);echo json_encode(array('error' => '缺少文件、分片索引或文件名'));}?>

merge_chunks.php

这个是用来合并分片的,当前端完成上传分片的操作,前端会异步告诉服务器你已经完成所有分片的上传,接下来将每个分片名告诉合并程序完成所有分片的合并,合并之后就是一个完整的视频文件。

<?php// 设置允许跨域访问header("Access-Control-Allow-Origin: *");header("Access-Control-Allow-Methods: POST");header("Content-Type: application/json");// 获取请求体中的文件名$data = json_decode(file_get_contents("php://input") , true);$fileName = isset($data['fileName']) ? $data['fileName'] : null;if ($fileName) {$uploadDir = './uploads/';$finalFilePath = $uploadDir . $fileName;$totalChunks = count(glob($uploadDir . $fileName . '.*'));// 检查是否所有分片都已上传if ($totalChunks > 0) {// 所有分片都已上传,开始合并$finalFile = fopen($finalFilePath, 'wb');// 逐个读取分片并写入最终文件for ($i = 0; $i < $totalChunks; $i++) {$chunkFilePath = $uploadDir . $fileName . '.' . $i;$chunkFile = fopen($chunkFilePath, 'rb');stream_copy_to_stream($chunkFile, $finalFile);fclose($chunkFile);unlink($chunkFilePath); // 删除已合并的分片}fclose($finalFile);http_response_code(200);echo json_encode(array('success' => '文件合并成功','filePath' => $finalFilePath));} else {http_response_code(400);echo json_encode(array('error' => '没有上传的分片'));}} else {http_response_code(400);echo json_encode(array('error' => '缺少文件名'));}
?>

程序目录

请自行创建 uploads 目录。
在这里插入图片描述

作者

TANKING

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.cpky.cn/p/9770.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

Wagtail安装运行并结合内网穿透实现公网访问本地网站界面

文章目录 前言1. 安装并运行Wagtail1.1 创建并激活虚拟环境 2. 安装cpolar内网穿透工具3. 实现Wagtail公网访问4. 固定的Wagtail公网地址 正文开始前给大家推荐个网站&#xff0c;前些天发现了一个巨牛的 人工智能学习网站&#xff0c; 通俗易懂&#xff0c;风趣幽默&#xf…

外包干了3个月,技术倒退明显...

先说情况&#xff0c;大专毕业&#xff0c;18年通过校招进入湖南某软件公司&#xff0c;干了接近6年的功能测试&#xff0c;今年年初&#xff0c;感觉自己不能够在这样下去了&#xff0c;长时间呆在一个舒适的环境会让一个人堕落!而我已经在一个企业干了四年的功能测试&#xf…

centos7安装mongodb

1.下载压缩包 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.22.tgz 解压 tar -zxvf mongodb-linux-x86_64-rhel70-4.4.22.tgz 移动到 /usr/local/mongodb mv mongodb-linux-x86_64-rhel70-4.4.22 /usr/local/mongodb 2.进入mongodb进行配置 cd mong…

【茶话数据结构】查找最短路径——Dijkstra算法详解(保姆式详细图解,步步紧逼,保你学会)

&#x1f4af; 博客内容&#xff1a;【茶话数据结构】查找最短路径——Dijkstra算法详解 &#x1f600; 作  者&#xff1a;陈大大陈 &#x1f989;所属专栏&#xff1a;数据结构笔记 &#x1f680; 个人简介&#xff1a;一个正在努力学技术的准前端&#xff0c;专注基础和实…

【开源】JAVA+Vue.js实现医院门诊预约挂号系统

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 功能性需求2.1.1 数据中心模块2.1.2 科室医生档案模块2.1.3 预约挂号模块2.1.4 医院时政模块 2.2 可行性分析2.2.1 可靠性2.2.2 易用性2.2.3 维护性 三、数据库设计3.1 用户表3.2 科室档案表3.3 医生档案表3.4 医生放号…

(十八)devops持续集成开发——使用docker安装部署jenkins服务

前言 本节内容介绍如何使用docker容器来部署安装jenkins流水线服务。关于docker容器的安装本节内容不做介绍。请读者提前安装。 正文 ①使用docker查找jenkins官方镜像 ② 拉取jenkins官方镜像jenkins/jenkins&#xff0c;选择一个最新稳定版本&#xff0c;避免一些插件不兼…