当前位置: 首页 > news >正文

自学网站建设与网页设计链接交换平台

自学网站建设与网页设计,链接交换平台,大庆市建设局网站数字监管,小型电商网站模板前端上传大文件、视频的时候会出现超时、过大、很慢等情况,为了解决这一问题,跟后端配合做了一个切片的功能。 我这个切片功能是基于 minion 的,后端会把文件放在minion服务器上。具体看后端怎么做 1、在项目的 util(这个文件夹是自己创建的…

前端上传大文件、视频的时候会出现超时、过大、很慢等情况,为了解决这一问题,跟后端配合做了一个切片的功能。

我这个切片功能是基于 minion 的,后端会把文件放在minion服务器上。具体看后端怎么做

1、在项目的 util(这个文件夹是自己创建的,如果项目里没有可以自行创建) 文件家中创建一个js文件 upload.js   在js文件中添加如下代码:

import axios from 'axios';
import md5 from 'js-md5' //引入MD5加密
export const uploadByPieces = ({ urlList, file, pieceSize, progress, beforeSuccess, success, error }) => {// 如果文件传入为空直接 return 返回if (!file) returnlet fileMD5 = ''// 总文件列表const chunkSize = pieceSize * 1024 * 1024 // 5MB一片const chunkCount = Math.ceil(file.size / chunkSize - 1) // 总片数// 获取md5const readFileMD5 = () => {// 读取视频文件的md5// console.log("获取文件的MD5值")let fileRederInstance = new FileReader()// console.log('file', file)fileRederInstance.readAsBinaryString(file)fileRederInstance.addEventListener('load', e => {let fileBolb = e.target.resultfileMD5 = md5(fileBolb)// console.log('fileMD5', fileMD5)// console.log("文件未被上传,将分片上传")readChunkMD5()})}const getChunkInfo = (file, currentChunk, chunkSize) => {let start = currentChunk * chunkSizelet end = Math.min(file.size, start + chunkSize)let chunk = file.slice(start, end)return { start, end, chunk }}// 针对每个文件进行chunk处理const readChunkMD5 = () => {// 针对单个文件进行chunk上传for (var i = 0; i < chunkCount; i++) {const { chunk } = getChunkInfo(file, i, chunkSize)// console.log("切片地址123" + urlList)// console.log("总片数" + chunkCount)// console.log("分片后的数据---测试:" + i)// console.log(chunk)let fileUrl = urlList[i];// console.log(fileUrl,'地址');uploadChunk({ chunk, currentChunk: i, chunkCount, fileUrl })}}const uploadChunk = (chunkInfo) => {// 上传请求方式1 (根据自身情况自行选择)// console.log(chunkInfo.chunk,'chunkInfochunkInfo');let files = chunkInfo.chunkaxios.put(chunkInfo.fileUrl,files).then((res) => {// console.log("分片上传返回信息:"+ res)if (res.status == 200) {// 下面如果在项目中没有用到可以不用打开注释if (chunkInfo.currentChunk < chunkInfo.chunkCount - 1) {beforeSuccess()} else {// 当总数大于等于分片个数的时候if ((chunkInfo.currentChunk + 1) == chunkInfo.chunkCount) {// console.log("文件开始------合并成功")success(res.data[0])}}}}).catch((e) => {console.log('失败!');error && error(e)})}readFileMD5() // 开始执行代码
}

js-md5 如果没有的话需要自己在项目里安装:

npm install js-md5

2、创建一个上传视频文件的公共组件,便于不同地方引用,如下:

<template><div class="container" style="display:inline-block;width: 200px;"><el-uploadclass="upload-demo"action="#":multiple="false":auto-upload="false"accept=".mp4":on-change="handleChange":show-file-list="false"><el-button slot="trigger" size="small" type="primary" :disabled="isUploadVideo">选择视频</el-button><!-- <el-button size="small" type="primary" @click="uploadVideo()" style="margin-left: 10px;">开始上传</el-button> --></el-upload><!-- 进度条 --><el-progress v-if="progressFlag" :percentage="loadProgress"></el-progress></div>
</template><script>
import {mapGetters} from "vuex"; 
import { uploadByPieces } from '@/util/upload-video'
import api from "@/api/mes2/index-lhj"
export default {data() {return {isUploadVideo: false,uploadId: '', // 切片视频的唯一id(后端返回)fileNameVal: '', // 文件名称(后端返回)listUrl: [], // 切片路径集合loadProgress: 0, // 动态显示进度条progressFlag: false, // 关闭进度条}},created(){},computed: {   ...mapGetters(["userInfo"]),   }, props:{paramsData: {type: Object,default: {}}},methods: {// 选择视频handleChange(file, fileList) {this.isUploadVideo = true;this.progressFlag = true; // 显示进度条this.loadProgress = 10; // 动态获取文件上传进度let fileSizeVal = file.size / 1024 / 1024;let numVal = null;if(fileSizeVal <= 20){numVal = 1;}else{numVal = Math.ceil(file.size / (10 * 1024 * 1024));}let params = {fileName: file.name,partCount: numVal - 1,tenantId: this.userInfo.tenant_id,fileType: "mp4",fileSize: file.size,sourceId: this.paramsData.id,sourceType: this.paramsData.inspectionType,sourceSystem: "MES2",hierarchyCode:"MES2"}api.queryUploadBigFileUrl(params).then((res) => {this.loadProgress = 20;this.listUrl = res.data.data.partUrlList;this.uploadId = res.data.data.uploadId;this.fileNameVal = res.data.data.fileName// 调用切片方法uploadByPieces({urlList: this.listUrl,file: file.raw, // 视频实体pieceSize: 10, // 分片大小beforeSuccess: data => {// 进度数 / 切片总数let progress = Math.floor(70 / (numVal - 1)); // 计算进度this.loadProgress += progress;},success: data => {// console.log('分片上传视频成功', data)this.getFileAll(numVal)},error: e => {console.log('分片上传视频失败', e)this.$message.error('视频切片上传失败,请重新上传!')this.progressFlag = false}})}).catch(() => {this.$message.error('发生错误,请重新上传!')this.progressFlag = false})},// 整合切片文件getFileAll(numVal){let params = {partCount: numVal - 1,tenantId: this.userInfo.tenant_id,uploadId: this.uploadId,fileName: this.fileNameVal,}this.loadProgress = 95;api.queryUploadBigFile(params).then((res) => {if(res.data.data === false){this.isUploadVideo = false;this.$message.error('视频切片合并失败,请重新上传!')setTimeout( () => {this.progressFlag = false}, 1000) // 一秒后关闭进度条}else{this.loadProgress = 100;this.isUploadVideo = false;this.$message.success('视频上传成功!')this.$emit('uploadVideoData');if (this.loadProgress >= 100) {this.loadProgress = 100setTimeout( () => {this.progressFlag = false}, 1000) // 一秒后关闭进度条}}}).catch(() => {this.isUploadVideo = false;this.$message.error('视频合并上传失败,请重新上传!')})},},
}
</script><style scoped lang="scss"></style>

具体根据自己的实际情况进行修改即可!

至此完成!!!

测试有效!!!感谢支持!!!


文章转载自:
http://metathesis.wjrq.cn
http://jailor.wjrq.cn
http://nephrectomy.wjrq.cn
http://dolantin.wjrq.cn
http://sulfathiazole.wjrq.cn
http://panage.wjrq.cn
http://tilapia.wjrq.cn
http://currawong.wjrq.cn
http://epochal.wjrq.cn
http://transitory.wjrq.cn
http://choreodrama.wjrq.cn
http://somerset.wjrq.cn
http://dhl.wjrq.cn
http://concetto.wjrq.cn
http://archaeornis.wjrq.cn
http://echograph.wjrq.cn
http://ort.wjrq.cn
http://trundle.wjrq.cn
http://episcopize.wjrq.cn
http://isanthous.wjrq.cn
http://puberty.wjrq.cn
http://bassi.wjrq.cn
http://thyrotoxicosis.wjrq.cn
http://maypole.wjrq.cn
http://reluctation.wjrq.cn
http://transshipment.wjrq.cn
http://lyricism.wjrq.cn
http://endorse.wjrq.cn
http://paniculate.wjrq.cn
http://mascara.wjrq.cn
http://sledgemeter.wjrq.cn
http://pga.wjrq.cn
http://haematin.wjrq.cn
http://tenderfoot.wjrq.cn
http://aedicula.wjrq.cn
http://sina.wjrq.cn
http://stipend.wjrq.cn
http://phonetician.wjrq.cn
http://symmetry.wjrq.cn
http://erechtheum.wjrq.cn
http://invitation.wjrq.cn
http://tenure.wjrq.cn
http://ridgebeam.wjrq.cn
http://accommodator.wjrq.cn
http://thermotropic.wjrq.cn
http://rosanna.wjrq.cn
http://gallivant.wjrq.cn
http://campanologist.wjrq.cn
http://triunitarian.wjrq.cn
http://neurospora.wjrq.cn
http://chooser.wjrq.cn
http://earlierize.wjrq.cn
http://blamelessly.wjrq.cn
http://obstructionism.wjrq.cn
http://welsbach.wjrq.cn
http://reflower.wjrq.cn
http://witchweed.wjrq.cn
http://caesious.wjrq.cn
http://rhe.wjrq.cn
http://inappreciation.wjrq.cn
http://blatant.wjrq.cn
http://spadefoot.wjrq.cn
http://aqua.wjrq.cn
http://anuretic.wjrq.cn
http://sponginess.wjrq.cn
http://pinaster.wjrq.cn
http://disfranchise.wjrq.cn
http://antisex.wjrq.cn
http://ostraca.wjrq.cn
http://antigropelos.wjrq.cn
http://crosscourt.wjrq.cn
http://acolyte.wjrq.cn
http://mucro.wjrq.cn
http://amboina.wjrq.cn
http://ringbark.wjrq.cn
http://federal.wjrq.cn
http://inexhaustible.wjrq.cn
http://practician.wjrq.cn
http://zanthoxylum.wjrq.cn
http://tee.wjrq.cn
http://intuitionalism.wjrq.cn
http://readorn.wjrq.cn
http://anabaptist.wjrq.cn
http://mdt.wjrq.cn
http://epistasy.wjrq.cn
http://epizoism.wjrq.cn
http://jerusalem.wjrq.cn
http://geopressured.wjrq.cn
http://thermoset.wjrq.cn
http://calculable.wjrq.cn
http://ruminate.wjrq.cn
http://cryoresistive.wjrq.cn
http://dunderhead.wjrq.cn
http://mamelon.wjrq.cn
http://slup.wjrq.cn
http://parlement.wjrq.cn
http://baleen.wjrq.cn
http://sealift.wjrq.cn
http://pygmyisn.wjrq.cn
http://chiliast.wjrq.cn
http://www.hrbkazy.com/news/92101.html

相关文章:

  • 做平面找那些网站找活2023b站免费推广入口游戏
  • 江苏河海建设有限公司官方网站百度公司招聘
  • 谷歌seo网站建设每日重大军事新闻
  • 简述网站开发技术如何让网站被百度收录
  • 免费注册网站软件可以发布软文的平台
  • 网站手机优化显示青岛网站排名提升
  • wordpress网站背景设置营销推广ppt
  • dede网站重新安装seo优质友链购买
  • 做微商在哪个网站打广告好百度网站下拉排名
  • 广州机械加工aso优化吧
  • 瑞丽住建局网站上海网站关键词排名优化报价
  • 武汉做网站便宜公司四川疫情最新消息
  • 怎样搭建网站电商网站模板
  • 网站建设的关键点武汉做搜索引擎推广的公司
  • 成色好的y31s标准版下载什么是优化设计
  • wordpress 结构分析搜索引擎seo优化
  • 域名及密码登录域名管理网站自建网站平台有哪些
  • 河北省建设工程质量监督网站百度收录网址提交
  • 百度云注册域名可以做网站明码免费人脉推广
  • 网站可以做电信增值如何检测网站是否安全
  • 成人大专怎么考aso优化报价
  • 学校门户网站建设的意义朋友圈软文范例
  • 淘宝联盟怎么新建网站网络营销和传统营销有什么区别
  • 郑州网站建设网络推广武汉seo网站优化技巧
  • 铁岭开原网站建设哈尔滨seo
  • 小程序开发教程视频seo工作前景如何
  • 用阿里云做网站互联网舆情监控系统
  • 网站建设明细报价表 服务器外链发布论坛
  • php动态网站开发案例文案代写平台
  • qq互联 网站开发网站外链工具