前端分片获取文件信息


dataContentLoaded:false, //标记文件内容是否加载完成
fileContent:'', // 用于存储文件内容的字符串
dataOffset: 0, //文件起始偏移
dataLength: 1000 * 1000, //每次请求的长度(自定义大小)


 async getDataDetail() {
      try {
        //后端请求接口,返回的是bytes类型
        const gtn = await getVal(this.dataPath, this.dataOffset, this.dataLength); 
        if (isEmpty(gtn.data.byte)) return;
        
        const byte = atob(gtn.data.byte).split('')
       .map((char) => char.charCodeAt(0));
        const newBytes = new Uint8Array(byte);
        const decoder = new TextDecoder('utf-8');
        const newContent = decoder.decode(newBytes);

        // 将新获取的分片内容追加到现有内容
        this.fileContent += newContent;
        // 更新dataOffset以请求下一部分
        this.dataOffset += this.dataLength;
        // 如果返回的内容长度小于请求的长度,表示已经到达文件末尾
        if (newBytes.length < this.dataLength) {
          this.dataContentLoaded = true;
        } else {
          // 继续请求下一部分
          setTimeout(() => this.getDataDetail(), 0);
        }
      } catch (error) {
        console.error('Error fetching file chunk:', error);
      }
    },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值