1.DownloadClient.ets code changed, Adaptation download service
Signed-off-by: zhoulisheng <635547767@qq.com>
This commit is contained in:
parent
ba8fa48c19
commit
6a13a03917
|
@ -18,7 +18,7 @@ import {RequestOption} from "../RequestOption"
|
|||
import {Md5} from "../../cache/Md5"
|
||||
import{FileUtils} from "../../cache/FileUtils"
|
||||
|
||||
import httpRequest from '@ohos.request';
|
||||
import loadRequest from '@ohos.request';
|
||||
|
||||
export class DownloadClient implements IDataFetch {
|
||||
loadData(request: RequestOption, onCompleteFunction, onErrorFunction) {
|
||||
|
@ -26,37 +26,106 @@ export class DownloadClient implements IDataFetch {
|
|||
let requestUrl = request.loadSrc as string;
|
||||
if (requestUrl.startsWith("http") || requestUrl.startsWith("https")) {
|
||||
let filename = Md5.hashStr(request.generateDataKey);
|
||||
let allpath = request.networkCacheFolder + "/" + filename + ".img";
|
||||
let downloadFolder = request.getFilesPath() + "/" +request.networkCacheFolder;
|
||||
let allpath = request.getFilesPath() + "/" + filename + ".img";
|
||||
|
||||
if(FileUtils.getInstance().exist(allpath)){
|
||||
FileUtils.getInstance().deleteFile(allpath)
|
||||
}
|
||||
|
||||
let desc = filename + ".img";
|
||||
|
||||
var downloadConfig = {
|
||||
url: (request.loadSrc as string),
|
||||
filePath: allpath
|
||||
filePath: allpath,
|
||||
header: {}
|
||||
enableMetered: true,
|
||||
enableRoaming: true,
|
||||
description: desc,
|
||||
networkType: 1,
|
||||
title: filename,
|
||||
};
|
||||
httpRequest.download(downloadConfig)
|
||||
|
||||
let loadTask = null;
|
||||
loadRequest.download(downloadConfig)
|
||||
.then((downloadTask) => {
|
||||
downloadTask.query((err, data) => {
|
||||
if (err) {
|
||||
onErrorFunction(err)
|
||||
return;
|
||||
}
|
||||
downloadTask.on('progress', (uploadedSize, totalSize)=>{
|
||||
let percent = Math.round(((uploadedSize*1.0)/(totalSize*1.0))*100)+"%"
|
||||
|
||||
if(downloadTask){
|
||||
loadTask = downloadTask;
|
||||
|
||||
loadTask.on('progress', (err, receivedSize, totalSize)=>{
|
||||
let percent = Math.round(((receivedSize*1.0)/(totalSize*1.0))*100)+"%"
|
||||
if(request.progressFunc) {
|
||||
request.progressFunc(percent);
|
||||
}
|
||||
});
|
||||
downloadTask.on('complete', () => {
|
||||
let downloadPath = request.getCachesPath() + "/" + allpath;
|
||||
|
||||
loadTask.on('complete', () => {
|
||||
let downloadPath = allpath;
|
||||
request.downloadFilePath = downloadPath;
|
||||
let arraybuffer = FileUtils.getInstance()
|
||||
.readFilePic(downloadPath)
|
||||
let arraybuffer = FileUtils.getInstance().readFilePic(downloadPath)
|
||||
onCompleteFunction(arraybuffer);
|
||||
FileUtils.getInstance()
|
||||
.deleteFile(downloadPath);
|
||||
FileUtils.getInstance().deleteFile(downloadPath);
|
||||
|
||||
loadTask.off('complete',()={
|
||||
loadTask = null;
|
||||
})
|
||||
|
||||
loadTask.off('pause',()={
|
||||
})
|
||||
|
||||
loadTask.off('remove',()={
|
||||
|
||||
})
|
||||
|
||||
loadTask.off('progress',()={
|
||||
|
||||
})
|
||||
|
||||
loadTask.off('fail',()={
|
||||
|
||||
})
|
||||
})
|
||||
downloadTask.on('fail', (err) => {
|
||||
onErrorFunction("DownloadClient Download task fail. err =" + err)
|
||||
|
||||
loadTask.on('pause',()={
|
||||
})
|
||||
})
|
||||
|
||||
loadTask.on('remove',()={
|
||||
|
||||
})
|
||||
|
||||
loadTask.off('fail',(err)=>{
|
||||
onErrorFunction('DownloadClient Download task fail err ='+err)
|
||||
if(loadTask){
|
||||
loadTask.remove().then(result =>{
|
||||
loadTask.off('complete',()={
|
||||
|
||||
})
|
||||
|
||||
loadTask.off('pause',()={
|
||||
})
|
||||
|
||||
loadTask.off('remove',()={
|
||||
|
||||
})
|
||||
|
||||
loadTask.off('progress',()={
|
||||
|
||||
})
|
||||
|
||||
loadTask.off('fail',()={
|
||||
|
||||
})
|
||||
loadTask = null
|
||||
}).catch(err =>{
|
||||
loadTask = null;
|
||||
console.log('DownloadClient Download task fail err ='+err);
|
||||
})
|
||||
})
|
||||
|
||||
}else{
|
||||
onErrorFunction('DownloadClient downloadTask dismiss!')
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
onErrorFunction(err)
|
||||
|
|
Loading…
Reference in New Issue