适用条件:
openharmony开发
4.0 release版本,对应能力API10
一直不断尝试,一会用官方提供的上传文件,一会用第三方库的axios都不行,
一会报错‘没权限,一会报错’路径错误,还有报错‘401参数错误的,反正各种报错都遇到了,官方文档说的不够仔细,示例也不够清楚,痛苦
首先一定要动态获取权限,很重要 (即使在modules.json5文件里面已经说明了,在页面上还是要判断一下有咩有,然后申请权限)
写在onPageShow里,我没有加是否存在权限的判断,需要自行判断,有权限就不弹了,没权限在向用户申请,弹弹窗
import abilityAccessCtrl, { Context, PermissionRequestResult } from '@ohos.abilityAccessCtrl';let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); let context: Context = getContext(this) as common.UIAbilityContext; await atManager.requestPermissionsFromUser(context, ["ohos.permission.WRITE_MEDIA","ohos.permission.READ_MEDIA", ], (err: BusinessError, data: PermissionRequestResult) => {if (err) {console.log(`requestPermissionsFromUser fail, err->${JSON.stringify(err)}`);} else {console.info('这是data:' + JSON.stringify(data));console.info('data permissions:' + data.permissions);console.info('data authResults:' + data.authResults); //如果是0则是有权限了} })看到这个弹窗,才算权限申请生效了
其次,一定要进行文件的复制,这样才能获取到文件(在下面代码)
方法一,使用官方提供的上传文件请求
import fs from '@ohos.file.fs';
import request from '@ohos.request';//fileUri是文件选择器返回的路径,格式如下: "datashare:///media/image/45"async uploadImg(fileUri: string) {/ 获取应用文件路径const context = getContext(this) as common.UIAbilityContext;let cacheDir = context.cacheDir; console.log('cacheDir', cacheDir); //=>沙箱路径 /应用文件目录 /data/storage/el2/base/haps/entry/cache//文件名称,下面这两种方法都可以,原则就是保持名字随机性// let segments = fileUri.split('/')// let fileName = segments[segments.length-1]const fileName = Date.now() + '.jpg'//复制这一步一定不能少 ,简而言之,就是把我们的内部文件拿到外部暴露出来,let realUri = context.cacheDir + "/" + fileName //计划复制到的目标路径try {let file = fs.openSync(fileUri);fs.copyFileSync(file.fd, realUri)} catch (err) {console.info('err:' + JSON.stringify(err));}// // 鸿蒙自带请求let uploadTask: request.UploadTask;let uploadConfig: request.UploadConfig = {url: 'http://upload', //需要手动替换为真实服务器地址header: { 'Content-Type': 'multipart/form-data', token: AppStorage.get('token') },method: "POST",files: [{ filename: fileName, name: "image", uri: `internal://cache/${fileName}`, type: "jpg" }],
//注意这里,files里面的url一定是'internal://cache/'和文件名的拼接,不要拼其他路径!!!data: [],};//一定要定义类型,这里的类型定义真的很让人头大interface path{filePath:string}interface bodyType{ code:stringsuccess:stringdata:path}interface resType{body:bodyType}try {request.uploadFile(context, uploadConfig).then((data: request.UploadTask) => {uploadTask.on('headerReceive', (header:object) => {const picRes:resType = header as resTypeconsole.log('上传头picRes', picRes.body )})uploadTask.on("complete", () => {promptAction.showToast({ message: '上传成功!', duration: 2000 })// 上传成功调接口})}).catch((err: BusinessError) => {console.error(`Failed to request the upload. Code: ${err.code}, message: ${err.message}`);});} catch (err) {console.error(`Failed to request the upload. err: ${JSON.stringify(err)}`);}}
方法二,使用第三方库 @ohos/axios,创建formdata对象请求
import fs from '@ohos.file.fs';
import axios, { AxiosError, AxiosProgressEvent, AxiosResponse, FormData, InternalAxiosRequestConfig } from '@ohos/axios'//fileUri是文件选择器返回的路径,格式如下: "datashare:///media/image/45"async uploadImg(fileUri: string) {/ 获取应用文件路径const context = getContext(this) as common.UIAbilityContext;let cacheDir = context.cacheDir; console.log('cacheDir', cacheDir); //=>沙箱路径 /应用文件目录 /data/storage/el2/base/haps/entry/cache//文件名称,下面这两种方法都可以,原则就是保持名字随机性// let segments = fileUri.split('/')// let fileName = segments[segments.length-1]const fileName = Date.now() + '.jpg'//复制这一步一定不能少 ,简而言之,就是把我们的内部文件拿到外部暴露出来,let realUri = context.cacheDir + "/" + fileName //计划复制到的目标路径try {let file = fs.openSync(fileUri);fs.copyFileSync(file.fd, realUri)} catch (err) {console.info('err:' + JSON.stringify(err));}// 第三方库axios请求let formData = new FormData();formData.append('image', `internal://cache/${fileName}`);// formData.append('image', 'internal://cache/'+fileName);//记得替换真实请求地址axios.post<string, AxiosResponse<string>, FormData>('http://upload', formData, {headers: {'Content-Type': 'multipart/form-data','token': AppStorage.get('token')},context: getContext(this),onUploadProgress: (progressEvent: AxiosProgressEvent): void => {console.info(progressEvent && progressEvent.loaded && progressEvent.total ? Math.ceil(progressEvent.loaded / progressEvent.total * 100) + '%' : '0%');},}).then((res: AxiosResponse) => {interface pathType{filePath:string}interface dataType{data:pathType}const data: dataType = res.data as dataTypeconst path = data.data.filePathconsole.info("request result" ,path);}).catch((error: AxiosError) => {console.error("error:" + JSON.stringify(error));})}
别夸我,我真是个菩萨,经历了九九八十一难,搞了两天才弄好的上传方法
如果描述哪里不正确,不完整,及时告知我噢