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 {Md5} from "../../cache/Md5"
|
||||||
import{FileUtils} from "../../cache/FileUtils"
|
import{FileUtils} from "../../cache/FileUtils"
|
||||||
|
|
||||||
import httpRequest from '@ohos.request';
|
import loadRequest from '@ohos.request';
|
||||||
|
|
||||||
export class DownloadClient implements IDataFetch {
|
export class DownloadClient implements IDataFetch {
|
||||||
loadData(request: RequestOption, onCompleteFunction, onErrorFunction) {
|
loadData(request: RequestOption, onCompleteFunction, onErrorFunction) {
|
||||||
|
@ -26,37 +26,106 @@ export class DownloadClient implements IDataFetch {
|
||||||
let requestUrl = request.loadSrc as string;
|
let requestUrl = request.loadSrc as string;
|
||||||
if (requestUrl.startsWith("http") || requestUrl.startsWith("https")) {
|
if (requestUrl.startsWith("http") || requestUrl.startsWith("https")) {
|
||||||
let filename = Md5.hashStr(request.generateDataKey);
|
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 = {
|
var downloadConfig = {
|
||||||
url: (request.loadSrc as string),
|
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) => {
|
.then((downloadTask) => {
|
||||||
downloadTask.query((err, data) => {
|
|
||||||
if (err) {
|
if(downloadTask){
|
||||||
onErrorFunction(err)
|
loadTask = downloadTask;
|
||||||
return;
|
|
||||||
}
|
loadTask.on('progress', (err, receivedSize, totalSize)=>{
|
||||||
downloadTask.on('progress', (uploadedSize, totalSize)=>{
|
let percent = Math.round(((receivedSize*1.0)/(totalSize*1.0))*100)+"%"
|
||||||
let percent = Math.round(((uploadedSize*1.0)/(totalSize*1.0))*100)+"%"
|
|
||||||
if(request.progressFunc) {
|
if(request.progressFunc) {
|
||||||
request.progressFunc(percent);
|
request.progressFunc(percent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
downloadTask.on('complete', () => {
|
|
||||||
let downloadPath = request.getCachesPath() + "/" + allpath;
|
loadTask.on('complete', () => {
|
||||||
|
let downloadPath = allpath;
|
||||||
request.downloadFilePath = downloadPath;
|
request.downloadFilePath = downloadPath;
|
||||||
let arraybuffer = FileUtils.getInstance()
|
let arraybuffer = FileUtils.getInstance().readFilePic(downloadPath)
|
||||||
.readFilePic(downloadPath)
|
|
||||||
onCompleteFunction(arraybuffer);
|
onCompleteFunction(arraybuffer);
|
||||||
FileUtils.getInstance()
|
FileUtils.getInstance().deleteFile(downloadPath);
|
||||||
.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) => {
|
.catch((err) => {
|
||||||
onErrorFunction(err)
|
onErrorFunction(err)
|
||||||
|
|
Loading…
Reference in New Issue