首页 > 其他分享 >前端处理大文件/视频分片上传,断点续传

前端处理大文件/视频分片上传,断点续传

时间:2023-11-07 11:13:24浏览次数:26  
标签:断点续传 res data formData file 分片 上传 append

1、html部分,项目使用的是ant-design-vue组件库;spark-md5进行加密上传

 

 

2、使用变量:(通过上传成功的分片数与总分片数对比判断上传是否完成)

 

 

3、主要方法:

1、 查询当前文件是否上传

 

 

2、获取文件的MD5编码

// 获取文件的MD5编码
    computedSliceMd5 (file) {
      this.file = file
      this.fileType = file.type
      this.blobSlice =
        File.prototype.mozSlice ||
        File.prototype.webkitSlice ||
        File.prototype.slice
      this.spark = new SparkMD5.ArrayBuffer()
      this.chunkSpark = new SparkMD5.ArrayBuffer()
      this.currChunkSpark = new SparkMD5.ArrayBuffer()
      this.totalFileReader = new FileReader()
      const checkFileNameOk = true
      if (checkFileNameOk) {
        this.chunks = Math.ceil(this.file.size / this.chunkSize)
        this.totalFileReader.onload = e => {
          this.spark.append(e.target.result)
          this.currChunkSpark.append(e.target.result)
          this.chunkSparkMd5 = this.currChunkSpark.end()
          this.tmpDataList[this.tmpDataList.length - 1].partMd5 = this.chunkSparkMd5 // 修改当前塞进去的片段的md5
          this.currentChunk++
          if (this.currentChunk < this.chunks) {
            this.loadNext()
          } else {
            // 文件的MD5身份标识
            this.identifier = this.spark.end()
            // 查询当前文件是否已上传
            this.checkIsUpload().then(res => { // 根据实际项目调用
              const md5Arr = []
              if (res.data.success) {
                res.data.data.part.forEach(item => {
                  if (item) {
                    md5Arr.push(item.filePartMd5)
                  }
                })
              }
              this.uploadedCount = md5Arr.length
              const isUpload = res.data.success && !md5Arr.length && res.data.data.full.fileName
              if (isUpload) {
                this.$message.error(`您上传的${file.name}已经上传过,请选中其他文件!`)
                this.uploading = false
                return
              }
              for (let i = 0; i < this.tmpDataList.length; i++) {
                if (md5Arr.indexOf(this.tmpDataList[i].partMd5) === -1) {
                  const requestCount = 0
                  const formData = new FormData()
                  formData.append('data', this.tmpDataList[i].file)
                  formData.append('fileName', this.uploadFileName)
                  formData.append('index', this.tmpDataList[i].currentNum)
                  formData.append('isTrancode', 0)
                  formData.append('md5', this.identifier)
                  formData.append('partMd5', this.tmpDataList[i].partMd5)
                  formData.append('size', this.file.size)
                  formData.append('total', this.chunks)
                  formData.append('platformCode', 'CMRH_IVARS')
                  if (!this.uploadFile(formData, requestCount)) {
                    this.$message.error(`您上传的${this.file.name}上传失败,如有需要请重新上传...`)
                    this.uploading = false
                    break
                  }
                }
              }
            })
          }
        }
        this.totalFileReader.onerror = function () {
          this.$message.error('读取文件出错,请重试')
          this.uploading = false
        }
        this.loadNext()
      } else {
      }
    },

3、文件切片

// 文件切片
    loadNext () {
      // 对每一片分片进行md5加密
      this.start = this.currentChunk * this.chunkSize
      this.end =
        this.start + this.chunkSize >= this.file.size
          ? this.file.size
          : this.start + this.chunkSize
      const pieceFile = this.blobSlice.call(this.file, this.start, this.end)
      pieceFile.name = this.file.name
      const tmpObj = {
        file: pieceFile,
        currentSize: this.end - this.start,
        currentNum: this.currentChunk + '',
        partMd5: this.chunkSpark.end()
      }
      this.tmpDataList.push(tmpObj)
      this.totalFileReader.readAsArrayBuffer(pieceFile)
    },

4、上传分片

// 上传分片
    async uploadFile (param, count) {
      const res = await axios.post(config.getVodUrl('/media/upload'), param, { headers: { 'Authorization': this.nucToken } })
      if (res.data.success) {
        this.uploadedCount++
        return true
      } else {
        count++
        if (count < 3) {
          this.uploadFile(param, count)
        } else {
          // 当前分片上传3次还未成功,不再上传,直接判定失败
          this.$message.error(res.data.message)
          this.uploading = false
          return true
        }
      }
    },

5、完成

 

 

 

参考文章:http://blog.ncmem.com/wordpress/2023/11/07/%e5%89%8d%e7%ab%af%e5%a4%84%e7%90%86%e5%a4%a7%e6%96%87%e4%bb%b6-%e8%a7%86%e9%a2%91%e5%88%86%e7%89%87%e4%b8%8a%e4%bc%a0%ef%bc%8c%e6%96%ad%e7%82%b9%e7%bb%ad%e4%bc%a0/

欢迎入群一起讨论

 

 

标签:断点续传,res,data,formData,file,分片,上传,append
From: https://www.cnblogs.com/songsu/p/17814569.html

相关文章

  • 大文件上传最全方案:秒传、断点续传、分片上传
    前言文件上传是一个老生常谈的话题了,在文件相对比较小的情况下,可以直接把文件转化为字节流上传到服务器,但在文件比较大的情况下,用普通的方式进行上传,这可不是一个好的办法,毕竟很少有人会忍受,当文件上传到一半中断后,继续上传却只能重头开始上传,这种让人不爽的体验。那有没有比较好......
  • 文件上传
    .htaccess定义:htaccess文件是Apache服务器中的一个配置文件,它负责相关目录下的网页配置。.htaccess主要的作用有:URL重写、自定义错误页面、MIME类型配置以及访问权限控制等。主要体现在伪静态的应用、图片防盗链、自定义404错误页面、阻止/允许特定IP/IP段、目录浏览与主页、禁......
  • 上传大文件的解决方案
    需求:项目要支持大文件上传功能,经过讨论,初步将文件上传大小控制在500M内,因此自己需要在项目中进行文件上传部分的调整和配置,自己将大小都以501M来进行限制。 第一步:前端修改由于项目使用的是BJUI前端框架,并没有使用框架本身的文件上传控件,而使用的基于jQuery的Uploadify文件上......
  • Git的使用--如何将本地项目上传到Github
    https://blog.csdn.net/NHB456789/article/details/131596777https://blog.csdn.net/Charles_Tian/article/details/80842439......
  • 大文件上传 问题解决三种方案
    最近遇见一个需要上传百兆大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现。在某些业务中,大文件上传是一个比较重要的交互场景,如上传入库比较大的Excel表格数据、上传影音文件等。如果文件体积比较大,或者网络条件不好时,上传的时间会......
  • 使用Feign接口实现文件上传的解决方案
    原文链接:使用Feign接口实现文件上传的解决方案一般的情况下,后端有个微服务,暴露出一个文件上传的restful接口给前端,前端调用该接口获取上传后的链接以及osskey值完成上传。假设提供restful接口的这个服务叫做A,现在有个微服务B有个本地文件,需要将本地文件调用A文件文件上传接口上传......
  • vue3.0 + ts 实现上传工厂(oss与cos)
    概述将上传基类命名为MOS(MineObjectStorage)mos.ts代码import{MosType}from'./mosConfig'import{Loading}from'../loading'import{typeBinaryFile,typeMosFile}from'./fileUtil'importtype{PathTemplate}from'./pathTempla......
  • 文件上传
    关于文件上传修改myproject/myproject/urls.py添加路由#上传文件path('upload/list/',upload.upload_list),新建myproject/app01/views/upload.pyfromdjango.shortcutsimportrender,HttpResponsedefupload_list(request):ifrequest.method=="GE......
  • WEB端实现文件夹上传
    webkitdirectory属性。这个属性加上之后,就是选择文件夹,然后根据自己业务上传至后台;前端代码:<formaction="${ctxPath}/invoice/uploadFolder"method="post"enctype="multipart/form-data"> <inputname="chooseFolder"type="file"id="c......
  • 模拟攻击beescms框架网站,并且一步一步渗透测试,上传shell,连接蚁剑,拿到对方网站根目录
    打开网站发现它是beescms框架搭建的网站,一言不合直接用webpathbrute扫描发现了管理员登录页面尝试任意用户名密码登录发现不太行,直接暴力破解,先burp抓数据包发现有4个参数有user,password,code,submit,把submit=ture修改为submit=false验证码就不会刷新了就是284c。接下来......