HarmonyOS 图片上传失败

/*图片选择
选取单个图片
 */
import { Logger } from '@neu/common';

import { BusinessError } from '@ohos.base';
import picker from '@ohos.file.picker';
import { textAttribute12_300_dark } from './CommonModifier';
import common from '@ohos.app.ability.common';
import httpClient from '../../api/request';

const TAG = ['UploadPhoto']

/*
//http请求config打印
{"method":"POST","readTimeout":90000,"connectTimeout":90000,"url":"./add_refund_attach","multiFormDataList":[{"name":"refundAttach","contentType":"image/jpeg","filePath":"file://media/Photo/3/IMG_1719818021_002/IMG_20240701_151201.jpg"}],"header":{"deviceType":"iPhone","deviceId":"03ca323e-77cb-45ac-9519-f80c8a1464c3","deviceModel":"iPhone4.21.2","sessionId":"34d97ff324a389f6cc5edc98481fd6b9","version":"42102","timeStamp":"1720418411794","deviceKey":"AFF1EC04FF6A61B27EF124747AAF4FD7","content-Type":"multipart/form-data"},"expectDataType":1,"extraData":{"param":"TOZLDlPGCNUai84qeL3QmD0/U8dZ5WJoElY2rwoxVCc="}}

//http请求error报错
{"code":2300026,"message":"Failed to open/read local data from file/application"}



*/
@Component
export struct UploadPhoto {
  select?: (url: string) => void
  @State uri: string = ''

  build() {
    RelativeContainer() {
      Image($r("app.media.add_photo"))
        .width(30)
        .height(30)
        .margin({ top: 13 })
        .id('icon')
        .alignRules({
          middle: { anchor: '__container__', align: HorizontalAlign.Center },
          top: { anchor: '__container__', align: VerticalAlign.Top }
        })
        .visibility(!this.uri ? Visibility.Visible : Visibility.None)
      Text($r('app.string.order_detail_upload'))
        .attributeModifier(textAttribute12_300_dark)
        .id('text')
        .margin({ top: 7 })
        .alignRules({
          middle: { anchor: '__container__', align: HorizontalAlign.Center },
          top: { anchor: 'icon', align: VerticalAlign.Bottom }
        })
        .visibility(!this.uri ? Visibility.Visible : Visibility.None)
      Image(this.uri)
        .id('photo')
        .alignRules({
          left: { anchor: '__container__', align: HorizontalAlign.Start },
          top: { anchor: '__container__', align: VerticalAlign.Top }
        }).visibility(this.uri ? Visibility.Visible : Visibility.None)
      Image($r('app.media.icon_orange_btn_delete'))
        .width(20)
        .height(20)
        .id('close')
        .alignRules({
          middle: { anchor: '__container__', align: HorizontalAlign.End },
          center: { anchor: '__container__', align: VerticalAlign.Top }
        })
        .visibility(this.uri ? Visibility.Visible : Visibility.None)
        .onClick(() => {
          this.uri = ''
          this.select?.(this.uri)
        })
    }
    .width(74)
    .height(73)
    .borderColor('#003D69')
    .borderStyle(BorderStyle.Dotted)
    .borderWidth(this.uri ? 0 : 1)
    .onClick(() => {
      !this.uri && this.getFileAssetsFromType();
    })
    .borderRadius(6)
  }

  // 接口采用promise异步返回形式,传入可选参数DocumentSelectOptions对象,返回选择文件的uri数组。
  async getFileAssetsFromType() {
    const photoSelectOptions = new picker.PhotoSelectOptions(); // 创建图片-音频类型文件-预览的图库选项实例
    // 选择媒体文件类型和选择媒体文件的最大数目
    photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE; // 选择媒体文件类型为Image

    photoSelectOptions.maxSelectNumber = 1; // 选择媒体文件的最大数目
    let context = getContext(this) as common.Context;
    // TODO:知识点:创建图库选择器实例,调用photoViewPicker.select()接口拉起图库界面进行文件选择,文件选择成功后,返回photoSelectResult结果集。
    const photoViewPicker = new picker.PhotoViewPicker(context);
    photoViewPicker.select(photoSelectOptions)
      .then((photoSelectResult: picker.PhotoSelectResult) => {
        const uris = photoSelectResult.photoUris;
        this.uri = uris[0] || ''
        // this.select?.(this.uri)
        httpClient.upload({
          url: '/add_refund_attach',
          multiFormDataList: [{
            name: 'refundAttach',
            contentType: 'image/jpeg',
            filePath: this.uri,
          }]
        });
      }).catch((err: BusinessError) => {
      Logger.info(TAG)
      return;
    })
  }
}
HarmonyOS
4天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

filePath需要转换为沙箱文件路径

//  参考demo:
let uris: Array<string> = [];
const photoViewPicker = new picker.PhotoViewPicker();
const photoSelectOptions = new picker.PhotoSelectOptions();
photoSelectOptions.maxSelectNumber = 1; // 选择媒体文件的最大数目
photoViewPicker.select(photoSelectOptions).then((photoSelectResult: picker.PhotoSelectResult) => {
  uris = photoSelectResult.photoUris;
  console.info('photoViewPicker.select to file succeed and uris are:' + uris);
  let file = fs.openSync(uris[0], fs.OpenMode.READ_ONLY);

  let file2 = fs.openSync(this.filesDir + '/test.jpg', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  //先创建有读写权限的文件,再把不可读写的文件复制过来
  fs.copyFileSync(file.fd, file2.fd);
  // 关闭文件
  fs.closeSync(file);
  fs.closeSync(file2);

}).catch((err: BusinessError) => {
  console.error('Invoke photoViewPicker.select failed', 'code is:'+ `${err.code}`,' message is:'+ `${err.message}`);
})

请尝试转换path为应用的沙箱路径后再上传图片

分享
微博
QQ
微信
回复
4天前
相关问题
HarmonyOS 上传图片失败
170浏览 • 1回复 待解决
HarmonyOS 图片上传
64浏览 • 1回复 待解决
HarmonyOS 上传图片问题
106浏览 • 1回复 待解决
HarmonyOS 上传图片异常
179浏览 • 1回复 待解决
HarmonyOS图片上传相关问题
500浏览 • 1回复 待解决
HarmonyOS 图片上传httpRequest格式问题
128浏览 • 1回复 待解决
HarmonyOS如何实现图片上传
539浏览 • 1回复 待解决
HarmonyOS web上传文件和图片
290浏览 • 1回复 待解决
使用FilePick上传uri失败,报错201
1803浏览 • 1回复 待解决
HarmonyOS Web 图片上传功能失效
258浏览 • 1回复 待解决
HarmonyOS 如何上传本地图片
187浏览 • 1回复 待解决
HarmonyOS 怎么对图片进行压缩上传
84浏览 • 1回复 待解决
怎么实现后台上传图片
2360浏览 • 1回复 待解决
HarmonyOS 图片上传文件过大如何处理
112浏览 • 1回复 待解决
HarmonyOS 如何使用Axios框架上传图片
43浏览 • 1回复 待解决
HarmonyOS 如何压缩图片之后再上传
50浏览 • 1回复 待解决
HarmonyOS 关于从相册中选择图片上传
42浏览 • 1回复 待解决