From e7268eff37ca14dbaf26d6eb4ff2fff9cd22eb6b Mon Sep 17 00:00:00 2001 From: lloyd <353627866@qq.com> Date: Wed, 20 Aug 2025 18:23:12 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=AE=9E=E7=8E=B0=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E5=92=8C=E4=B8=8B=E8=BD=BD=E5=8A=9F=E8=83=BD=E3=80=91=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.en.md | 6 +++--- README.md | 6 +++--- build-profile.json5 | 3 ++- .../src/main/ets/download/RequestDownload.ets | 3 ++- .../src/main/ets/download/RequestFiles.ets | 4 ++-- .../src/main/ets/upload/RequestUpload.ets | 17 ++++++++++++----- .../src/main/ets/utils/FileUtils.ets | 4 +++- oh-package.json5 | 1 - 8 files changed, 27 insertions(+), 17 deletions(-) diff --git a/README.en.md b/README.en.md index 6d04aba..01c106b 100644 --- a/README.en.md +++ b/README.en.md @@ -103,8 +103,8 @@ This sample shows how to use the APIs provided by @ohos.request to create upload 1. The sample app is supported only on Huawei phones running the standard system. -2. The HarmonyOS version must be HarmonyOS 5.0.4 Release or later. +2. The HarmonyOS version must be HarmonyOS 5.0.5 Release or later. -3. The DevEco Studio version must be DevEco Studio 5.0.4 Release or later. +3. The DevEco Studio version must be DevEco Studio 5.0.5 Release or later. -4. The HarmonyOS SDK version must be HarmonyOS 5.0.4 Release or later. +4. The HarmonyOS SDK version must be HarmonyOS 5.0.5 Release or later. diff --git a/README.md b/README.md index 9b91289..069b270 100644 --- a/README.md +++ b/README.md @@ -92,8 +92,8 @@ ohos.permission.INTERNET:允许应用在上传下载场景中访问网络 1.本示例仅支持标准系统上运行,支持设备:华为手机。 -2.HarmonyOS系统:HarmonyOS 5.0.4 Release及以上。 +2.HarmonyOS系统:HarmonyOS 5.0.5 Release及以上。 -3.DevEco Studio版本:DevEco Studio 5.0.4 Release及以上。 +3.DevEco Studio版本:DevEco Studio 5.0.5 Release及以上。 -4.HarmonyOS SDK版本:HarmonyOS 5.0.4 Release SDK及以上。 +4.HarmonyOS SDK版本:HarmonyOS 5.0.5 Release SDK及以上。 diff --git a/build-profile.json5 b/build-profile.json5 index 284a4c6..e2ff76c 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -4,7 +4,8 @@ { "name": "default", "signingConfig": "default", - "compatibleSdkVersion": "5.0.4(16)", + "compatibleSdkVersion": "5.0.5(17)", + "targetSdkVersion": "5.0.5(17)", "runtimeOS": "HarmonyOS" } ], diff --git a/features/uploadanddownload/src/main/ets/download/RequestDownload.ets b/features/uploadanddownload/src/main/ets/download/RequestDownload.ets index 32d68cd..3df0e7e 100644 --- a/features/uploadanddownload/src/main/ets/download/RequestDownload.ets +++ b/features/uploadanddownload/src/main/ets/download/RequestDownload.ets @@ -20,7 +20,6 @@ import { request } from '@kit.BasicServicesKit'; import { logger } from '../utils/Logger'; import { TOAST_BOTTOM, TASK_MAX, TASK_PAUSE_MSG, TASK_NET_PAUSE_MSG, TASK_RESUME_MSG, TASK_NET_RESUME_MSG } from '../utils/Constants'; -const uiContext: UIContext | undefined = AppStorage.get('uiContext'); const TAG: string = 'RequestDownload'; let isNetPause = false; // [Start request_down_load] @@ -43,6 +42,7 @@ class RequestDownload { async downloadFilesBackground(folder: string, files: Array) { logger.info(TAG, 'downloadFiles'); + const uiContext: UIContext | undefined = AppStorage.get('uiContext'); this.context = uiContext?.getHostContext() as common.UIAbilityContext; files.forEach((item: string) => { this.waitList.push([folder, item]); @@ -122,6 +122,7 @@ class RequestDownload { action: request.agent.Action.DOWNLOAD, mode: request.agent.Mode.FOREGROUND }); + const uiContext: UIContext | undefined = AppStorage.get('uiContext'); if (tasks.length > 0) { uiContext?.getPromptAction().showToast({ message: $r('app.string.have_download_task_tips'), bottom: TOAST_BOTTOM }); return; diff --git a/features/uploadanddownload/src/main/ets/download/RequestFiles.ets b/features/uploadanddownload/src/main/ets/download/RequestFiles.ets index 8719a52..cd03379 100644 --- a/features/uploadanddownload/src/main/ets/download/RequestFiles.ets +++ b/features/uploadanddownload/src/main/ets/download/RequestFiles.ets @@ -19,7 +19,6 @@ import { FileModel } from './model/FileModel'; import { logger } from '../utils/Logger'; import { urlUtils } from '../utils/UrlUtils'; -const uiContext: UIContext | undefined = AppStorage.get('uiContext'); const TAG: string = 'RequestFiles'; const FILE_LENGTH: number = 2; @@ -27,6 +26,7 @@ class Request { async requestFiles() { let httpRequest = http.createHttp(); let files: Array = []; + const uiContext: UIContext | undefined = AppStorage.get('uiContext'); let context: common.UIAbilityContext = uiContext?.getHostContext() as common.UIAbilityContext; let url = await urlUtils.getUrl(context); let header: Record = { @@ -83,7 +83,7 @@ class Request { logger.info(TAG, `files = ${JSON.stringify(files)}`); return files; } catch (err) { - logger.info(TAG, `error: ${JSON.stringify(err)}`); + logger.error(TAG, `error: ${JSON.stringify(err)}`); httpRequest.destroy(); return []; } diff --git a/features/uploadanddownload/src/main/ets/upload/RequestUpload.ets b/features/uploadanddownload/src/main/ets/upload/RequestUpload.ets index 3ef4ad8..647a5bd 100644 --- a/features/uploadanddownload/src/main/ets/upload/RequestUpload.ets +++ b/features/uploadanddownload/src/main/ets/upload/RequestUpload.ets @@ -22,7 +22,6 @@ import { logger } from '../utils/Logger'; import { MediaUtils } from '../utils/MediaUtils'; import { BackgroundTaskState, UPLOAD_TOKEN, TOAST_BOTTOM, TASK_MAX } from '../utils/Constants'; -const uiContext: UIContext | undefined = AppStorage.get('uiContext'); const TAG: string = 'RequestUpload'; const HEADER: Record = { 'Content-Type': 'multipart/form-data' }; @@ -62,6 +61,10 @@ class Upload { async uploadFilesBackground(fileUris: Array): Promise { logger.info(TAG, `uploadFiles begin, ${JSON.stringify(fileUris)}`); + const uiContext: UIContext | undefined = AppStorage.get('uiContext'); + if (!uiContext) { + logger.error(TAG, `uploadFilesBackground uiContext is undefined`); + } this.context = uiContext?.getHostContext() as common.UIAbilityContext; if (fileUris.length === 0) { return; @@ -124,6 +127,10 @@ class Upload { action: request.agent.Action.UPLOAD, mode: request.agent.Mode.FOREGROUND }); + const uiContext: UIContext | undefined = AppStorage.get('uiContext'); + if (!uiContext) { + logger.error(TAG, `uploadFiles uiContext is undefined`); + } if (tasks.length > 0) { uiContext?.getPromptAction().showToast({ message: $r('app.string.have_upload_task_tips'), bottom: TOAST_BOTTOM }); return; @@ -148,7 +155,7 @@ class Upload { callback(100, true); this.cancelTask(); }); - this.uploadTask.on('failed', this.failedCallback = async (progress: request.agent.Progress) => { + this.uploadTask.on('failed', this.failedCallback = async (_progress: request.agent.Progress) => { if (this.uploadTask) { let taskInfo = await request.agent.touch(this.uploadTask.tid, UPLOAD_TOKEN); logger.info(TAG, `fail, resean = ${taskInfo.reason}, faults = ${JSON.stringify(taskInfo.faults)}`); @@ -191,7 +198,7 @@ class Upload { await this.resume(); AppStorage.setOrCreate('backTaskState', BackgroundTaskState.RUNNING); } else { - logger.info(TAG, 'this task state is error'); + logger.error(TAG, 'this task state is error'); } } @@ -206,7 +213,7 @@ class Upload { try { await this.backgroundTask.pause(); } catch (err) { - logger.info(TAG, `pause fail,err= ${JSON.stringify(err)}`); + logger.error(TAG, `pause fail,err= ${JSON.stringify(err)}`); } } // [End pause] @@ -222,7 +229,7 @@ class Upload { try { await this.backgroundTask.resume(); } catch (err) { - logger.info(TAG, `resume fail,err= ${JSON.stringify(err)}`); + logger.error(TAG, `resume fail,err= ${JSON.stringify(err)}`); } } // [End resume] diff --git a/features/uploadanddownload/src/main/ets/utils/FileUtils.ets b/features/uploadanddownload/src/main/ets/utils/FileUtils.ets index 7bff7b7..a10a960 100644 --- a/features/uploadanddownload/src/main/ets/utils/FileUtils.ets +++ b/features/uploadanddownload/src/main/ets/utils/FileUtils.ets @@ -17,7 +17,6 @@ import { common } from '@kit.AbilityKit'; import { fileIo } from '@kit.CoreFileKit'; import { logger } from '../utils/Logger'; -const uiContext: UIContext | undefined = AppStorage.get('uiContext'); const TAG: string = 'FileUtil'; const ALBUMS: string[] = ['Pictures', 'Videos', 'Others']; @@ -26,6 +25,7 @@ class FileUtil { } async initDownloadDir(): Promise { + const uiContext: UIContext | undefined = AppStorage.get('uiContext'); let context: common.UIAbilityContext = uiContext?.getHostContext() as common.UIAbilityContext; logger.info(TAG, `initDownloadDir cacheDir=${context.cacheDir}`); try { @@ -43,6 +43,7 @@ class FileUtil { } async clearFolder(folderName: string): Promise { + const uiContext: UIContext | undefined = AppStorage.get('uiContext'); let context: common.UIAbilityContext = uiContext?.getHostContext() as common.UIAbilityContext; try { let files: string[] = fileIo.listFileSync(`${context.cacheDir}/${folderName}`); @@ -56,6 +57,7 @@ class FileUtil { } async listFiles(folderName: string): Promise> { + const uiContext: UIContext | undefined = AppStorage.get('uiContext'); let context: common.UIAbilityContext = uiContext?.getHostContext() as common.UIAbilityContext; let files: string[] = []; try { diff --git a/oh-package.json5 b/oh-package.json5 index 4d6ca63..847c6c4 100644 --- a/oh-package.json5 +++ b/oh-package.json5 @@ -2,7 +2,6 @@ "modelVersion": "5.0.0", "license": "", "devDependencies": { - "@ohos/hypium": "1.0.15" }, "author": "", "name": "uploadanddownload", -- Gitee