前端vue后端go实现大文件分片下载

news/2024/7/10 3:10:28 标签: 后端, vue

先获取文件大小,然后将文件切片传输给前端,前端再按顺序组合所有切片。

vue

    /** 下载数据 */
    handleDownloadInstance(id, instanceName) {
      if (this.downloadProgressisVisible) {
        this.$showError('已经有文件在下载!')
        return
      }
      this.$showInfo('开始下载!')
      getFileSize(id).then((response) => {
        this.downloadProgressisVisible = true
        const fileSize = response.size
        this.fileSize = fileSize
        this.downloadPercentage = 0
        this.currentSize = 0
        console.log(response)

        // 计算分片数和每片大小
        const chunkSize = 10 * 1024 * 1024 // 10 MB
        const totalChunks = Math.ceil(fileSize / chunkSize)

        // 保存所有分片的数据
        const allChunks = new Array(totalChunks)
        const self = this
        let num = 0
        // 下载所有分片
        function downloadChunks() {
          for (let i = 0; i < totalChunks; i++) {
            const data = {
              start: i * chunkSize,
              end: (i + 1) * chunkSize - 1
            }
            downloadInstanceChunk(id, data).then(response => {
              return response
            })
              .then(chunkBlob => {
                // 保存分片数据
                allChunks[i] = chunkBlob
                num += 1
                // console.log('下载完成', i)
                // 检查是否所有分片都下载完成
                if (num === totalChunks) {
                  mergeChunks()
                  self.$showSuccess('下载成功!')
                  self.downloadProgressisVisible = false
                }
                self.downloadPercentage = Math.floor((num) / totalChunks * 100)
                self.currentSize = num * chunkSize
              })
              .catch(error => {
                console.error('Error:', error)
                self.downloadProgressisVisible = false
                self.$showError('下载文件失败:' + error.data.message)
              })
          }
        }

        // 合并所有分片
        function mergeChunks() {
          // 创建一个 Blob 对象,包含所有分片数据
          const mergedBlob = new Blob(allChunks, { type: 'application/zip' })

          // 创建下载链接并模拟点击
          const downloadLink = document.createElement('a')
          downloadLink.href = URL.createObjectURL(mergedBlob)
          const fileName = `${instanceName}.zip`
          downloadLink.download = fileName
          downloadLink.click()
        }

        // 调用下载分片函数
        downloadChunks()
      }).catch(err => {
        console.log(err)
        this.$showError('下载文件失败:' + err.data.message)
        this.downloadProgressisVisible = false
      })
    },

go

func (handler *Handler) getFileSize(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
	instanceID, err := request.RetrieveNumericRouteVariableValue(r, "id")
	if err != nil {
		return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid instance identifier route variable", Err: err}
	}

	instance, err := handler.DataStore.Instance().Instance(uint(instanceID))
	if err == bolterrors.ErrObjectNotFound {
		return &httperror.HandlerError{StatusCode: http.StatusNotFound, Message: "Unable to find a instance with the specified identifier inside the database", Err: err}
	} else if err != nil {
		return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to find a instance with the specified identifier inside the database", Err: err}
	}

	// timeStr := instance.ReportTime.Format("2006-01-02 15:04:05")
	// timeStr = strings.Replace(timeStr, ":", "-", -1)
	// timeStr = strings.Replace(timeStr, " ", "-", -1)

	dirPath := instance.TempPath
	zipPath := instance.InstanceName + ".zip"

	zipPath = "/home/1.zip"
	dirPath = "/home/
	if !utils.DirExists(dirPath) {
		return &httperror.HandlerError{StatusCode: http.StatusNotFound, Message: "文件不存在", Err: fmt.Errorf("文件不存在")}
	}
	_, err = os.Stat(zipPath)
	if err != nil {
		err = utils.ZipDir(dirPath, zipPath, []string{".pcap"})
		if err != nil {
			return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "压缩文件失败", Err: fmt.Errorf("压缩文件失败")}
		}
	}
	fileInfo, err := os.Stat(zipPath)
	if err != nil {
		return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "os.Stat 失败", Err: err}
	}
	return response.JSON(w, map[string]int64{
		"size": fileInfo.Size(),
	})
}

func (handler *Handler) downloadInstanceChunk(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
	instanceID, err := request.RetrieveNumericRouteVariableValue(r, "id")
	if err != nil {
		return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid instance identifier route variable", Err: err}
	}

	start, err := request.RetrieveNumericRouteVariableValue(r, "start")
	if err != nil {
		return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid instance identifier route variable", Err: err}
	}
	end, err := request.RetrieveNumericRouteVariableValue(r, "end")
	if err != nil {
		return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid instance identifier route variable", Err: err}
	}

	instance, err := handler.DataStore.Instance().Instance(uint(instanceID))
	if err == bolterrors.ErrObjectNotFound {
		return &httperror.HandlerError{StatusCode: http.StatusNotFound, Message: "Unable to find a instance with the specified identifier inside the database", Err: err}
	} else if err != nil {
		return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to find a instance with the specified identifier inside the database", Err: err}
	}

	// timeStr := instance.ReportTime.Format("2006-01-02 15:04:05")
	// timeStr = strings.Replace(timeStr, ":", "-", -1)
	// timeStr = strings.Replace(timeStr, " ", "-", -1)

	dirPath := instance.TempPath
	zipPath := instance.InstanceName + ".zip"

	zipPath = "/home/1.zip"
	dirPath = "/home/test"
	if !utils.DirExists(dirPath) {
		return &httperror.HandlerError{StatusCode: http.StatusNotFound, Message: "文件不存在", Err: fmt.Errorf("文件不存在")}
	}

	// err = utils.ZipDir(dirPath, zipPath, []string{".pcap"})
	if err != nil {
		return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "压缩文件失败", Err: fmt.Errorf("压缩文件失败")}
	}
	fileInfo, err := os.Stat(zipPath)
	if err != nil {
		return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "os.Stat 失败", Err: err}
	}
	// 计算最后一片的范围
	if end == 0 || int64(end) > fileInfo.Size() {
		end = int(fileInfo.Size()) - 1
	}
	zipFile, err := os.Open(zipPath)
	if err != nil {
		return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "文件无法打开", Err: fmt.Errorf("文件无法打开")}
	}
	defer zipFile.Close()

	w.Header().Set("Content-Type", "application/zip")
	// w.Header().Set("Content-Type", "application/octet-stream")
	// w.Header().Set("Content-Length", strconv.FormatInt(fileInfo.Size(), 10))
	// 设置响应头部,指定传输的范围
	w.Header().Set("Content-Range", "bytes "+strconv.FormatInt(int64(start), 10)+"-"+strconv.FormatInt(int64(end), 10))
	w.WriteHeader(http.StatusPartialContent)

	// 将部分内容传输到客户端
	_, err = zipFile.Seek(int64(start), 0)
	if err != nil {
		return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to seek to the specified position", Err: err}
	}

	io.CopyN(w, zipFile, int64(end)-int64(start)+1)

	return response.Empty(w)
}


http://www.niftyadmin.cn/n/5395175.html

相关文章

IDEA环境下项目的模块右下角蓝色小方块缺失的解决方法

一、问题描述 该问题的具体表现就如上图一样&#xff0c;模块——”spring-ioc-xml-01“的应该有一个蓝色的方块。现在蓝色方块缺失了之后&#xff0c;我们需要让maven工程重新识别出这个模块。 二、问题分析 出现这种问题的原因&#xff1a;是当前的maven工程没有能够成功的识…

vue项目打包获取git commit信息并输出到打包后的指定文件夹中

需求背景&#xff1a; 前端项目经常打包&#xff0c;发包部署&#xff0c;为了方便测试及运维发现问题时与正确commit信息对比 实现方式&#xff1a; 使用Node.js的child_process模块来执行git命令 实现步骤&#xff1a; 1.在package.json的同级目录下新建一个version.js文件。…

腾讯云服务器4核8G配置性能如何?

腾讯云4核8G服务器支持多少人在线访问&#xff1f;支持25人同时访问。实际上程序效率不同支持人数在线人数不同&#xff0c;公网带宽也是影响4核8G服务器并发数的一大因素&#xff0c;假设公网带宽太小&#xff0c;流量直接卡在入口&#xff0c;4核8G配置的CPU内存也会造成计算…

NeurIPS 2023 Spotlight | VoxDet:基于3D体素表征学习的新颖实例检测器

本文提出基于3D体素表征学习的新颖实例检测器VoxDet。给定目标实例的多视图&#xff0c;VoxDet建立该实例的三维体素表征。在更加杂乱的测试图片上&#xff0c;VoxDet使用体素匹配算法检测目标实例。实验表明&#xff0c;VoxDet中的三维体素表征与匹配比多种二维特征与匹配要更…

Data Leakage and Evaluation Issues inMicro-Expression Analysis 阅读笔记

IEEE Transactions on Affective Computing上的一篇文章&#xff0c;做微表情识别&#xff0c;阅读完做个笔记。本文讨论了Data Leakage对模型准确度评估的影响&#xff0c;及如何融合多个微表情数据集&#xff0c;从而提升模型的准确度。工作量非常饱满&#xff0c;很认真&…

论文浅尝 | 一种基于对比微调的脚本事件预测生成方法

笔记整理&#xff1a;柴智华&#xff0c;东南大学硕士&#xff0c;研究方向为事件预测 链接&#xff1a;https://arxiv.org/pdf/2212.03496.pdf 1. 动机 脚本事件预测任务是指在给定上下文时&#xff0c;预测后续事件。这需要推断事件之间相关性的能力。最近的工作试图通过使用…

2023年09月CCF-GESP编程能力等级认证C++编程五级真题解析

本文收录于专栏《C++等级认证CCF-GESP真题解析》,专栏总目录・点这里 一、单选题(共15题,共30分) 第1题 近年来,线上授课变得普遍,很多有助于改善教学效果的设备也逐渐流行,其中包括比较常用的手写板,那么它属于哪类设备?( ) A:输入 B:输出 C:控制 D:记录 答…

亿道丨三防平板丨加固平板丨工业平板丨选购三大要素?

在当今的市场中&#xff0c;企业在复杂多变的大环境中面临着许多挑战和机遇。其中包括尽管消费者对产品的需求不断增长&#xff0c;但劳动力短缺仍在继续的问题。大部分企业正在采用更具成本效益的方法&#xff0c;并希望利用他们获得的硬件和软件做更多的事情&#xff0c;因此…