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);
}
},
前端分片获取文件信息
于 2024-07-08 16:01:55 首次发布