From 0f471cb44c94a5ecf3ffae9979fb4b3e9f132617 Mon Sep 17 00:00:00 2001 From: liugang9704 <2745340733@qq.com> Date: Mon, 30 Jun 2025 11:40:50 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=A2=9EFAQ=EF=BC=9A=E5=A6=82?= =?UTF-8?q?=E4=BD=95=E4=BB=8E=E5=9B=BE=E5=BA=93=E9=80=89=E6=8B=A9=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E5=B9=B6=E4=B8=8A=E4=BC=A0=E5=88=B0=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entry/src/main/ets/pages/ImageUpload.ets | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 ImageKit/entry/src/main/ets/pages/ImageUpload.ets diff --git a/ImageKit/entry/src/main/ets/pages/ImageUpload.ets b/ImageKit/entry/src/main/ets/pages/ImageUpload.ets new file mode 100644 index 0000000..7b50e35 --- /dev/null +++ b/ImageKit/entry/src/main/ets/pages/ImageUpload.ets @@ -0,0 +1,100 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* +* FAQ:如何从图库选择图片并上传到服务器 +*/ + +// [Start ImageUpload] +import { request } from '@kit.BasicServicesKit'; +import { fileIo as fs } from '@kit.CoreFileKit'; +import { photoAccessHelper } from '@kit.MediaLibraryKit'; +import { common } from '@kit.AbilityKit'; + +@Entry +@Component +struct Index { + private openPhotoPicker() { + // 获取应用文件路径 + let context = this.getUIContext().getHostContext() as common.UIAbilityContext; + let cacheDir = context.cacheDir; + let photoPicker = new photoAccessHelper.PhotoViewPicker(); + photoPicker.select({ + MIMEType: photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE, + maxSelectNumber: 1 + }, (error, result) => { + if (result) { + result.photoUris.forEach((uri) => { + let file = fs.openSync(uri, fs.OpenMode.CREATE); + // 复制文件到缓存目录下 + fs.copyFileSync(file.fd, cacheDir + '/test.jpeg'); + this.uploadImage(['internal://cache/test.jpeg']); + }) + } + }) + } + + private uploadImage(paths: string[]) { + let allFiles = Array(); + let header = new Map(); + header.set('Content-Type', 'multipart/form-data'); + header.set('key2', 'value2'); + for (let i = 0; i < paths.length; i++) { + allFiles[i] = { + name: 'image' + i + '.jpeg', + filename: 'image' + i + '.jpeg', + uri: paths[i], + type: 'image' + } + } + let data: Array = [{ name: 'name', value: 'value' }]; + let uploadConfig: request.UploadConfig = { + url: 'http://XXX"', + header: header, + method: 'POST', + files: allFiles, + data: data + } + try { + request.uploadFile(this.getUIContext().getHostContext(), uploadConfig, (error, uploadTask) => { + if (uploadTask) { + uploadTask.on('progress', (uploadSize: number, totalSize: number) => { + console.info('progress,uploadedSize:' + uploadSize + ',totalSize:' + totalSize); + }) + } else { + console.info('upload failure:' + error); + } + }) + } catch (error) { + console.info('upload failure:' + error); + } + } + + build() { + Column() { + Button('选择图片上传') + .width('100%') + .onClick(() => { + this.openPhotoPicker(); + }) + } + .width('100%') + .height('100%') + .padding(16) + .justifyContent(FlexAlign.End) + } +} + +// [End ImageUpload] -- Gitee From d978e50edfce1c3304890873f537741da0a37b32 Mon Sep 17 00:00:00 2001 From: liugang9704 <2745340733@qq.com> Date: Mon, 30 Jun 2025 11:57:52 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ImageKit/entry/src/main/ets/pages/ImageUpload.ets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ImageKit/entry/src/main/ets/pages/ImageUpload.ets b/ImageKit/entry/src/main/ets/pages/ImageUpload.ets index 7b50e35..7d469ae 100644 --- a/ImageKit/entry/src/main/ets/pages/ImageUpload.ets +++ b/ImageKit/entry/src/main/ets/pages/ImageUpload.ets @@ -34,7 +34,7 @@ struct Index { photoPicker.select({ MIMEType: photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE, maxSelectNumber: 1 - }, (error, result) => { + }, (_, result) => { if (result) { result.photoUris.forEach((uri) => { let file = fs.openSync(uri, fs.OpenMode.CREATE); -- Gitee