HarmonyOS 鸿蒙Next下载文件的回调进度中sizes打印的是-1,为什么? 不应该是文件的总大小吗?
HarmonyOS 鸿蒙Next下载文件的回调进度中sizes打印的是-1,为什么? 不应该是文件的总大小吗?
try {
let config: request.agent.Config = {
action: request.agent.Action.DOWNLOAD,
url: url,
mode: request.agent.Mode.BACKGROUND,
gauge: true,
overwrite: true,
headers: {
'Content-Type': 'multipart/form-data',
'package_id': 'com.ce.ice',
'device_identification': 'harmonry_os_ice_demo',
'Authorization': axiosManager.getAuthorizationStr(),
'User-Agent': safetyTunnelManager.getTunnelCommonUserAget() // 设置安全隧道User-Agent
},
saveas: this.getIMImageSandboxPath(url),
proxy: safetyTunnelManager.getTunnelCommonProxy() //设置安全隧道代理
};
request.agent.create(getContext(), config)
.then((task: request.agent.Task) => { // 进度
let progressCallBack = (progress: request.agent.Progress) => {
console.info('progressCallBack - ' + JSON.stringify(progress))
};
task.on('progress',
progressCallBack) // 暂停
let pauseCallBack = (progress: request.agent.Progress) => {
console.info('pauseCallBack', JSON.stringify(progress));
};
task.on('pause',
pauseCallBack) // 完成
let completedCallBack = (progress: request.agent.Progress) => {
console.info('completedCallBack', JSON.stringify(progress));
let resultPath = this.getIMImageSandboxPath(url)
finish(resultPath)
};
task.on('completed',
completedCallBack) // 失败
let failedCallBack = (progress: request.agent.Progress) => {
console.info('failedCallBack', JSON.stringify(progress));
};
task.on('failed', failedCallBack)
task.start()
}).catch((err: BusinessError) => {
console.error(`Failed to create a download task, Code: ${err.code}, message: ${err.message}`);
})
} catch (err) {
console.error(`Failed to request the download. err: ${JSON.stringify(err)}`);
//打印的进度的日志如下
{
"state":
32, "index":
0, "processed":
2587088, "sizes":[-
1],
"extras":
{
"accept-ranges":
"bytes", "cache-control":
"max-age=8640000", "connection":
"keep-alive", "content-disposition":
"inline; filename=image0.jpg.jpg", "content-transfer-encoding":
"binary", "content-type":
"image/jpg", "date":
"Wed, 11 Sep 2024 01:11:35 GMT", "etag":
"cid:0/id:30866449/type:1/biz:0", "expires":
"Fri, 20 Dec 2024 01:11:35 GMT", "last-modified":
"Wed, 11 Sep 2024 01:11:35 GMT", "pragma":
"", "server":
"elb", "transfer-encoding":
"chunked", "vary":
"Accept-Encoding"
}
}
progressCallBack - {
"state": 32,
"index": 0,
"processed": 3688289,
"sizes": [-1],
"extras": {
"accept-ranges": "bytes",
"cache-control": "max-age=8640000",
"connection": "keep-alive",
"content-disposition": "inline; filename=image0.jpg.jpg",
"content-transfer-encoding": "binary",
"content-type": "image/jpg",
"date": "Wed, 11 Sep 2024 01:11:35 GMT",
"etag": "cid:0/id:30866449/type:1/biz:0",
"expires": "Fri, 20 Dec 2024 01:11:35 GMT",
"last-modified": "Wed, 11 Sep 2024 01:11:35 GMT",
"pragma": "",
"server": "elb",
"transfer-encoding": "chunked",
"vary": "Accept-Encoding"
}
}
更多关于HarmonyOS 鸿蒙Next下载文件的回调进度中sizes打印的是-1,为什么? 不应该是文件的总大小吗?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于HarmonyOS 鸿蒙Next下载文件的回调进度中sizes打印的是-1,为什么? 不应该是文件的总大小吗?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙系统中,下载文件的回调进度中sizes
打印为-1,通常表示系统尚未获取到文件的总大小信息,或者当前下载任务的状态不允许直接获取文件总大小。
sizes
字段通常用于指示下载任务中文件的总大小,但在某些情况下,例如文件是从流式数据源下载(如直播流),或者服务器未提供文件总大小信息时,该字段可能无法被正确填充。此外,如果下载任务刚开始或处于某种异常状态,也可能导致sizes
值不准确。
在鸿蒙系统的下载API设计中,可能会遇到此类情况,其中sizes
字段不能作为判断文件总大小的唯一依据。开发者应检查下载任务的状态码和错误信息,以确定是否成功获取到文件总大小信息。
如果sizes
字段始终为-1,且下载任务正常进行,可能需要通过其他方式获取文件大小,例如从HTTP响应头中的Content-Length
字段(如果服务器提供了该信息)。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html