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

上海建设网站的公司小程序推广

上海建设网站的公司,小程序推广,邯郸网站建设兼职,湖南网站设计费用先获取文件大小,然后将文件切片传输给前端,前端再按顺序组合所有切片。 vue /** 下载数据 */handleDownloadInstance(id, instanceName) {if (this.downloadProgressisVisible) {this.$showError(已经有文件在下载!)return}this.$showInfo(开始下载!)ge…

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

vue

    /** 下载数据 */handleDownloadInstance(id, instanceName) {if (this.downloadProgressisVisible) {this.$showError('已经有文件在下载!')return}this.$showInfo('开始下载!')getFileSize(id).then((response) => {this.downloadProgressisVisible = trueconst fileSize = response.sizethis.fileSize = fileSizethis.downloadPercentage = 0this.currentSize = 0console.log(response)// 计算分片数和每片大小const chunkSize = 10 * 1024 * 1024 // 10 MBconst totalChunks = Math.ceil(fileSize / chunkSize)// 保存所有分片的数据const allChunks = new Array(totalChunks)const self = thislet 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] = chunkBlobnum += 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 = falseself.$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 = fileNamedownloadLink.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.TempPathzipPath := 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.TempPathzipPath := 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.dtcms.com/wzjs/278445.html

相关文章:

  • 人民日报客户端是什么级别seo网站设计工具
  • 南阳公司网站制作2024年2月疫情又开始了吗
  • 做商城网站哪里买正规seo多少钱
  • 公司注册在上海的好处厦门seo关键词排名
  • 注册公司费用要多少aso优化服务
  • 微网站 地图百度站长工具怎么关闭
  • 南康网站建设公司智谋网站优化公司
  • 网站的在线支付怎么做网站seo优化多少钱
  • 怎样做网站测评营销型网站建设论文
  • 网站建设的程序seo技巧seo排名优化
  • 做餐饮系统网站建设中国楼市最新消息
  • 帝国cms 做的博客网站hao123主页
  • 女装网站建设的困难和不足怎么找当地的地推团队
  • 梧州单身相亲网站网络营销的三种方式
  • 临漳网站建设网站优化 福州
  • 中国最早做网站是谁信息流推广主要具有哪两大优势
  • 深圳集团网站开发公司seo排名优化工具推荐
  • 安徽城乡建设局网站win10系统优化软件
  • 汽配公司的网站要怎么做seo的内容有哪些
  • 做侵权电影网站什么后果济宁百度推广电话
  • 网站建设方案样本福建seo
  • h5做的网站如何连接数据库广州seo搜索
  • 百度怎么创建网站宁波seo网络推广定制
  • hao123主页我的上网主页seo优化软件大全
  • 做门户网站用什么模板好google推广有效果吗
  • 中国建设监理协会网站会员专区免费友情链接平台
  • 做微商网站需要哪些网络销售怎么找客源
  • APP网站开发联系电话百度竞价代理商
  • 网站开发运用到的相关技术什么网站可以发布广告
  • 网站开发写好了怎么发布金华网站建设