HarmonyOS上传文件以及权限授权

魏晓东  论坛元老 | 2024-6-24 07:21:49 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1782|帖子 1782|积分 5346

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
搞了一天,差不多搞完了,记载下,如今官方还有一些问题 会在下一个next版本修复(利用api9 版本 新版本自行参考修改)
0、实现结果

     鸿蒙实现图片上传
  1、准备页面

  1. build() {
  2.     Row() {
  3.       Column() {
  4.         Button('选择图片28')
  5.           .onClick(this.onChooseImage)
  6.         // 显示选中的图片(本地地址 非http)
  7.         ForEach(this.showChooseImage, (item, idnex) => {
  8.           Image(item).width(80).height(80).margin({bottom: 20})
  9.         })
  10.         // 为了方便展示选中的图片信息
  11.         ForEach(this.showListData, (item, idnex) => {
  12.           Text(item.path)
  13.         })
  14.       }
  15.       .width('100%')
  16.     }
  17.     .height('100%')
  18.   }
复制代码
2、授权逻辑

点击“选中图片28”之后,必要先判断是否有读写图片的权限。如果有走后面的流程,如果没有,必要体系弹窗让用户去授权。如果用户授权了,走后面的逻辑;如果用户不授权必要提示用户去体系内里的应用列表去授权(也可以不做这个操作)。
官方文档:文档中心
3、判断是否授权媒体

  1. import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
  2. let atManager = abilityAccessCtrl.createAtManager();
  3. import bundleManager from '@ohos.bundle.bundleManager';
  4. // let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT;
  5. let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION;
  6. // let tokenID = bundleFlags;
  7. let tokenID
  8. import common from '@ohos.app.ability.common';
  9. import picker from '@ohos.file.picker';
  10. import request from '@ohos.request';
  11. let context = getContext(this) as common.UIAbilityContext;
  12. /**
  13. * 对应用权限进行校验封装 我这边默认只能一个一个授权,多个授权自己封装
  14. */
  15. export const permissionsIsAllow = async (type: Permissions, cb:Function) => {
  16.   let bundleInfo: bundleManager.BundleInfo = await bundleManager.getBundleInfoForSelf(bundleFlags);
  17.   let appInfo: bundleManager.ApplicationInfo = bundleInfo.appInfo;
  18.   tokenID = appInfo.accessTokenId;
  19.   console.log('tokenID', tokenID)
  20.   try {
  21.       atManager.checkAccessToken(tokenID, type).then((data) => {
  22.       console.log(`${type} success, data->${JSON.stringify(data)}`);
  23.       if (data === 0) { // 已授权
  24.         cb()
  25.       } else { // 未授权
  26.         AlertDialog.show(
  27.           {
  28.             title: '温馨提示',
  29.             message: '您还没有授权',
  30.             autoCancel: false,
  31.             alignment: DialogAlignment.Bottom,
  32.             gridCount: 4,
  33.             primaryButton: {
  34.               value: '取消授权',
  35.               action: () => {
  36.                 console.info('Callback when the first button is clicked')
  37.                 AlertDialog.show(
  38.                   {
  39.                     title: '温馨提示',
  40.                     message: '必须要授权才能使用,是否前往应用进行授权',
  41.                     autoCancel: false,
  42.                     alignment: DialogAlignment.Bottom,
  43.                     gridCount: 4,
  44.                     primaryButton: {
  45.                       value: '取消',
  46.                       action: () => {
  47.                         console.warn('用户再次取消授权')
  48.                       }
  49.                     },
  50.                     secondaryButton: {
  51.                       value: '前往授权',
  52.                       action: () => {
  53.                         let wantInfo = {
  54.                           action: 'action.settings.app.info',
  55.                           parameters: {
  56.                             settingsParamBundleName: 'com.example.medicaltreatment' // 打开指定应用的详情页面
  57.                           }
  58.                         }
  59.                         context.startAbility(wantInfo).then((data) => {
  60.                           // ...
  61.                           console.info('前往授权页面成功', JSON.stringify(data))
  62.                         }).catch((err) => {
  63.                           // ...
  64.                           console.error('前往授权页面失败', JSON.stringify(err))
  65.                         })
  66.                       }
  67.                     }
  68.                   }
  69.                 )
  70.               }
  71.             },
  72.             secondaryButton: {
  73.               value: '确认授权',
  74.               action: () => {
  75.                 atManager.requestPermissionsFromUser(context, [type]).then((data) => {
  76.                   console.info("data:" + JSON.stringify(data));
  77.                   console.info("data permissions:" + data.permissions);
  78.                   console.info("data authResults:", JSON.stringify(data.authResults));
  79.                   let length: number = data.authResults.length;
  80.                   for (let i = 0; i < length; i++) {
  81.                     if (data.authResults[i] === 0) {
  82.                       // 用户授权,可以继续访问目标操作
  83.                       cb()
  84.                     } else {
  85.                       // 用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限
  86.                       return;
  87.                     }
  88.                   }
  89.                 }).catch((err) => {
  90.                   console.info("data:" + JSON.stringify(err));
  91.                 })
  92.               }
  93.             },
  94.             cancel: () => {
  95.               console.info('Closed callbacks')
  96.             }
  97.           }
  98.         )
  99.       }
  100.     }).catch((err) => {
  101.       console.warn(`${type} fail, err->${JSON.stringify(err)}`);
  102.     });
  103.   } catch(err) {
  104.     console.log(`catch err->${JSON.stringify(err)}`);
  105.   }
  106. }
复制代码
结果:


4、选择图片

  1. /**
  2. * 选择图片 单/多
  3. */
  4. export const chooseImage = (cb: Function, number: number) => {
  5.   try {
  6.     let PhotoSelectOptions = new picker.PhotoSelectOptions();
  7.     PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
  8.     PhotoSelectOptions.maxSelectNumber = number;
  9.     let photoPicker = new picker.PhotoViewPicker();
  10.     photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult) => {
  11.       console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult));
  12.       console.log('PhotoSelectResult111', PhotoSelectResult)
  13.       if (PhotoSelectResult && PhotoSelectResult.photoUris) {
  14.         const imgList = PhotoSelectResult.photoUris;
  15.         cb(imgList)
  16.       }
  17.     }).catch((err) => {
  18.       console.error('PhotoViewPicker.select failed with err: ' + err);
  19.     });
  20.   } catch (err) {
  21.     console.error('PhotoViewPicker failed with err: ' + err);
  22.   }
  23. }
复制代码
选中的图片的路径是这样的:

5、将datashare的路径进行转换

为什么要转换: 看官方文档:文档中心
  1. private onDealWithData = (fileUris: string[]) => {
  2.     console.log('appLog:fileUris:', JSON.stringify(fileUris));
  3.     this.showChooseImage = fileUris
  4.     this.onUploadImageFileList = []
  5.     const promises = fileUris.map(async item => await this.onPromiseTask(item));
  6.     Promise.all(promises)
  7.       .then(() => {
  8.         console.log("All tasks completed. Proceed to the next step.");
  9.         if (this.onUploadImageFileList.length > 0) {
  10.           this.onUploadFile(this.onUploadImageFileList);
  11.         } else {
  12.           console.log('onUploadImageFileList的长度为0')
  13.         }
  14.         // 在这里执行下一步操作
  15.       })
  16.       .catch(error => {
  17.         console.error("Error occurred:", error);
  18.       });
  19.   }
  20.   private onPromiseTask = (v) => {
  21.     return new Promise((resolve, reject) => {
  22.       fs.open(v, fs.OpenMode.READ_ONLY).then((file) => { // READ_ONLY READ_WRITE
  23.         const dateStr = (new Date().getTime()).toString()
  24.         let newPath = context.cacheDir + `/${dateStr}.png`;
  25.         fs.copyFile(file.fd, newPath).then(() => {
  26.           console.info("applog:copy file succeed");
  27.           let realUri = "internal://cache/"+newPath.split("cache/")[1];
  28.           const obj = {
  29.             filename: `${dateStr}.png`,
  30.             name: "files",
  31.             uri: realUri,
  32.             type: "png"
  33.           }
  34.           this.onUploadImageFileList.push(obj);
  35.           resolve(true)
  36.         }).catch((err) => {
  37.           console.info("applog:copy file failed with error message: " + err.message + ", error code: " + err.code);
  38.         });
  39.       }).catch((err) => {
  40.         console.info("applog:open file failed with error message: " + err.message + ", error code: " + err.code);
  41.       });
  42.     })
  43.   }
复制代码
转换好之后的文件路径是这样的:

6、上传到服务器

  1. /**
  2. * 上传图片
  3. */
  4. export const uploadImageListOrSingle = async (files, cb:Function) => {
  5.   const token = await PreferenceModel.getPreference('tokenInfo', 'token')
  6.   const uploadConfigData = []
  7.   console.info('转换之后的files', JSON.stringify(files))
  8.   files.forEach(v => {
  9.     const uploadConfig = {
  10.       url: 'https://xxx.xxx.xxx/api/v1.0/oss/upload',
  11.       header: {
  12.         'Hwkj-Custom-Client': 'PlatformOfWeb',
  13.         "Authorization": token ? 'Bearer ' + token : '',
  14.       },
  15.       method: "POST",
  16.       files: [v],
  17.       data: [{ name: "files", value: "files"}],
  18.     }
  19.     uploadConfigData.push(uploadConfig)
  20.   })
  21.   const promises = uploadConfigData.map(async item => await onGetImageUploadBackUrl(item));
  22.   Promise.all(promises)
  23.     .then((data) => {
  24.       const showList = []
  25.       data.forEach(v => {
  26.         showList.push(v[0])
  27.       })
  28.       cb(showList)
  29.     })
  30.     .catch(error => {
  31.       console.error("Error occurred1:", error);
  32.     });
  33. }
  34. const onGetImageUploadBackUrl = (uploadConfig) => {
  35.   let uploadTask;
  36.   return new Promise((resolve, reject) => {
  37.     try {
  38.       request.uploadFile( getContext(this), uploadConfig).then((data) => {
  39.         console.info('JSON.stringify(data)', JSON.stringify(data))
  40.         uploadTask = data;
  41.         let upProgressCallback = (data) => {
  42.           console.info("data1111:" + JSON.stringify(data));
  43.           resolve(data)
  44.         };
  45.         uploadTask.on('complete', upProgressCallback);
  46.       }).catch((err) => {
  47.         console.error('Failed to request the upload. Cause: ' + JSON.stringify(err));
  48.       });
  49.     } catch (err) {
  50.       console.error('applog:', JSON.stringify(err));
  51.       console.error('err.code : ' + err.code + ', err.message : ' + err.message);
  52.     }
  53.   })
  54. }
复制代码
6、显示选中的和上传接口返回的数据信息(结果图)

注意事项:

1、不知道为什么checkAccessToken永远走的.catch,不知道是代码原因还是原来这个api的原因。导致用户同意授权之后,下一次仍旧是未授权。如今还未办理,还在等官方的答复(已办理 上方的代码已经办理了)
2、大家可以看到 当通过上传接口返回的数据并不是服务器返回的数据而是官方返回的图片信息

我已经对这个问题给官方说了:
“我们这个上传接口会返回一个图片的url 但是如今执行complete之后 返回的并不是服务器返回的数据 这种情况怎么办呢?”
官方的答复是:

7、完整代码

1、页面ui代码

  1. import fs from '@ohos.file.fs';import common from '@ohos.app.ability.common';let context = getContext(this) as common.UIAbilityContext;import { permissionsIsAllow, chooseImage, uploadImageListOrSingle } from '../../utils'@Entry@Componentstruct AccountSettingIndex {  @State showListData: any[] = []  @State showChooseImage: any[] = []  @State onUploadImageFileList: any[] = []  private onChooseImage = () => {    permissionsIsAllow('ohos.permission.WRITE_MEDIA', () => { // READ_MEDIA      this.onChooseImageFin()    })  }  private onChooseImageFin = () => {    chooseImage((imgList) => {      this.onDealWithData(imgList)    }, 2)  }  private onDealWithData = (fileUris: string[]) => {
  2.     console.log('appLog:fileUris:', JSON.stringify(fileUris));
  3.     this.showChooseImage = fileUris
  4.     this.onUploadImageFileList = []
  5.     const promises = fileUris.map(async item => await this.onPromiseTask(item));
  6.     Promise.all(promises)
  7.       .then(() => {
  8.         console.log("All tasks completed. Proceed to the next step.");
  9.         if (this.onUploadImageFileList.length > 0) {
  10.           this.onUploadFile(this.onUploadImageFileList);
  11.         } else {
  12.           console.log('onUploadImageFileList的长度为0')
  13.         }
  14.         // 在这里执行下一步操作
  15.       })
  16.       .catch(error => {
  17.         console.error("Error occurred:", error);
  18.       });
  19.   }
  20.   private onPromiseTask = (v) => {
  21.     return new Promise((resolve, reject) => {
  22.       fs.open(v, fs.OpenMode.READ_ONLY).then((file) => { // READ_ONLY READ_WRITE
  23.         const dateStr = (new Date().getTime()).toString()
  24.         let newPath = context.cacheDir + `/${dateStr}.png`;
  25.         fs.copyFile(file.fd, newPath).then(() => {
  26.           console.info("applog:copy file succeed");
  27.           let realUri = "internal://cache/"+newPath.split("cache/")[1];
  28.           const obj = {
  29.             filename: `${dateStr}.png`,
  30.             name: "files",
  31.             uri: realUri,
  32.             type: "png"
  33.           }
  34.           this.onUploadImageFileList.push(obj);
  35.           resolve(true)
  36.         }).catch((err) => {
  37.           console.info("applog:copy file failed with error message: " + err.message + ", error code: " + err.code);
  38.         });
  39.       }).catch((err) => {
  40.         console.info("applog:open file failed with error message: " + err.message + ", error code: " + err.code);
  41.       });
  42.     })
  43.   }  private onUploadFile = async (fileUri) => {    uploadImageListOrSingle(fileUri, (data) => {      console.log('获取到的url是', typeof data ,data)      this.showListData = data    })  }  build() {    Row() {      Column() {        Button('选择图片28')          .onClick(this.onChooseImage)        // 显示选中的图片        ForEach(this.showChooseImage, (item, idnex) => {          Image(item).width(80).height(80).margin({bottom: 20})        })        // 为了方便展示选中的图片信息        ForEach(this.showListData, (item, idnex) => {          Text(item.path)        })      }      .width('100%')    }    .height('100%')  }}
复制代码
2、utils文件下面的三个方法
  1. import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
  2. let atManager = abilityAccessCtrl.createAtManager();
  3. import bundleManager from '@ohos.bundle.bundleManager';
  4. // let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT;
  5. let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION;
  6. // let tokenID = bundleFlags;
  7. let tokenID
  8. import common from '@ohos.app.ability.common';
  9. import picker from '@ohos.file.picker';
  10. import request from '@ohos.request';
  11. let context = getContext(this) as common.UIAbilityContext;
  12. /**
  13. * 对应用权限进行校验封装 我这边默认只能一个一个授权,多个授权自己封装
  14. */
  15. export const permissionsIsAllow = async (type: Permissions, cb:Function) => {
  16.   let bundleInfo: bundleManager.BundleInfo = await bundleManager.getBundleInfoForSelf(bundleFlags);
  17.   let appInfo: bundleManager.ApplicationInfo = bundleInfo.appInfo;
  18.   tokenID = appInfo.accessTokenId;
  19.   console.log('tokenID', tokenID)
  20.   try {
  21.       atManager.checkAccessToken(tokenID, type).then((data) => {
  22.       console.log(`${type} success, data->${JSON.stringify(data)}`);
  23.       if (data === 0) { // 已授权
  24.         cb()
  25.       } else { // 未授权
  26.         AlertDialog.show(
  27.           {
  28.             title: '温馨提示',
  29.             message: '您还没有授权',
  30.             autoCancel: false,
  31.             alignment: DialogAlignment.Bottom,
  32.             gridCount: 4,
  33.             primaryButton: {
  34.               value: '取消授权',
  35.               action: () => {
  36.                 console.info('Callback when the first button is clicked')
  37.                 AlertDialog.show(
  38.                   {
  39.                     title: '温馨提示',
  40.                     message: '必须要授权才能使用,是否前往应用进行授权',
  41.                     autoCancel: false,
  42.                     alignment: DialogAlignment.Bottom,
  43.                     gridCount: 4,
  44.                     primaryButton: {
  45.                       value: '取消',
  46.                       action: () => {
  47.                         console.warn('用户再次取消授权')
  48.                       }
  49.                     },
  50.                     secondaryButton: {
  51.                       value: '前往授权',
  52.                       action: () => {
  53.                         let wantInfo = {
  54.                           action: 'action.settings.app.info',
  55.                           parameters: {
  56.                             settingsParamBundleName: 'com.example.medicaltreatment' // 打开指定应用的详情页面
  57.                           }
  58.                         }
  59.                         context.startAbility(wantInfo).then((data) => {
  60.                           // ...
  61.                           console.info('前往授权页面成功', JSON.stringify(data))
  62.                         }).catch((err) => {
  63.                           // ...
  64.                           console.error('前往授权页面失败', JSON.stringify(err))
  65.                         })
  66.                       }
  67.                     }
  68.                   }
  69.                 )
  70.               }
  71.             },
  72.             secondaryButton: {
  73.               value: '确认授权',
  74.               action: () => {
  75.                 atManager.requestPermissionsFromUser(context, [type]).then((data) => {
  76.                   console.info("data:" + JSON.stringify(data));
  77.                   console.info("data permissions:" + data.permissions);
  78.                   console.info("data authResults:", JSON.stringify(data.authResults));
  79.                   let length: number = data.authResults.length;
  80.                   for (let i = 0; i < length; i++) {
  81.                     if (data.authResults[i] === 0) {
  82.                       // 用户授权,可以继续访问目标操作
  83.                       cb()
  84.                     } else {
  85.                       // 用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限
  86.                       return;
  87.                     }
  88.                   }
  89.                 }).catch((err) => {
  90.                   console.info("data:" + JSON.stringify(err));
  91.                 })
  92.               }
  93.             },
  94.             cancel: () => {
  95.               console.info('Closed callbacks')
  96.             }
  97.           }
  98.         )
  99.       }
  100.     }).catch((err) => {
  101.       console.warn(`${type} fail, err->${JSON.stringify(err)}`);
  102.     });
  103.   } catch(err) {
  104.     console.log(`catch err->${JSON.stringify(err)}`);
  105.   }
  106. }/**
  107. * 选择图片 单/多
  108. */
  109. export const chooseImage = (cb: Function, number: number) => {
  110.   try {
  111.     let PhotoSelectOptions = new picker.PhotoSelectOptions();
  112.     PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
  113.     PhotoSelectOptions.maxSelectNumber = number;
  114.     let photoPicker = new picker.PhotoViewPicker();
  115.     photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult) => {
  116.       console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult));
  117.       console.log('PhotoSelectResult111', PhotoSelectResult)
  118.       if (PhotoSelectResult && PhotoSelectResult.photoUris) {
  119.         const imgList = PhotoSelectResult.photoUris;
  120.         cb(imgList)
  121.       }
  122.     }).catch((err) => {
  123.       console.error('PhotoViewPicker.select failed with err: ' + err);
  124.     });
  125.   } catch (err) {
  126.     console.error('PhotoViewPicker failed with err: ' + err);
  127.   }
  128. }/** * 上传图片 */export const uploadImageListOrSingle = async (files, cb:Function) => {  const token = await PreferenceModel.getPreference('tokenInfo', 'token')  const uploadConfigData = []  console.info('转换之后的files', JSON.stringify(files))  files.forEach(v => {    const uploadConfig = {      url: 'https://xxx.xxx.com/api/v1.0/oss/upload',      header: {        'Hwkj-Custom-Client': 'PlatformOfWeb',        "Authorization": token ? 'Bearer ' + token : '',      },      method: "POST",      files: [v],      data: [{ name: "files", value: "files"}],    }    uploadConfigData.push(uploadConfig)  })  const promises = uploadConfigData.map(async item => await onGetImageUploadBackUrl(item));  Promise.all(promises)    .then((data) => {      const showList = []      data.forEach(v => {        showList.push(v[0])      })      cb(showList)    })    .catch(error => {      console.error("Error occurred1:", error);    });}const onGetImageUploadBackUrl = (uploadConfig) => {  let uploadTask;  return new Promise((resolve, reject) => {    try {      request.uploadFile( getContext(this), uploadConfig).then((data) => {        console.info('JSON.stringify(data)', JSON.stringify(data))        uploadTask = data;        let upProgressCallback = (data) => {          console.info("data1111:" + JSON.stringify(data));          resolve(data)        };        uploadTask.on('complete', upProgressCallback);      }).catch((err) => {        console.error('Failed to request the upload. Cause: ' + JSON.stringify(err));      });    } catch (err) {      console.error('applog:', JSON.stringify(err));      console.error('err.code : ' + err.code + ', err.message : ' + err.message);    }  })}
复制代码
上一章:HarmonyOS自界说标题栏-CSDN博客
下一章:HarmonyOS文件下载以及消息通知-CSDN博客

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

魏晓东

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表