HarmonyOS 有关图片/文件等上传的文档资料吗

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

请参考文档:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/app-file-upload-download-V5

示例参考:

import request from '@ohos.request';
import picker from '@ohos.file.picker';
import fs, { ReadOptions } from '@ohos.file.fs';
import common from '@ohos.app.ability.common';


// 获取应用文件路径
let context = getContext(this) as common.UIAbilityContext;
let cacheDir = context.cacheDir;
@Entry
@Component
struct Index4 {
  private openPhotoPicker() {
    let photoPicker = new picker.PhotoViewPicker();
    photoPicker.select({
      MIMEType: picker.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<request.File>()
    let header = new Map<Object, string>();
    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<request.RequestData> = [{ name: 'name', value: 'value' }];
    let uploadConfig: request.UploadConfig = {
      url: "http://XXX&#34;,
      header: header,
      method: 'POST',
      files: allFiles,
      data: data
    }
    try {
      request.uploadFile(getContext(this), uploadConfig, (error, uploadTask) => {
        if(uploadTask) {
          uploadTask.on('progress', (uploadedSize: number, totalSize: number) => {
            console.log("progress, uploadedSize: " + uploadedSize + ", totalSize: " + totalSize)
          })
        } else {
          console.log("upload failure: " + error)
        }
      });
    } catch (error) {
      console.log("upload failure: " + error)
    }
  }
  build() {
    Stack() {
      Column(){
      }
    }
    .width('100%')
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
3天前
相关问题
有关深色模式开发文档
1713浏览 • 1回复 待解决
HarmonyOS web上传文件图片
290浏览 • 1回复 待解决
HarmonyOS 图片上传文件过大如何处理
112浏览 • 1回复 待解决
HarmonyOS h5 web上传图片文件demo
33浏览 • 1回复 待解决
HarmonyOS 推送相关开发文档资料
52浏览 • 1回复 待解决
HarmonyOS 图片上传
67浏览 • 1回复 待解决
HarmonyOS 上传文件demo
21浏览 • 1回复 待解决
HarmonyOS 图片上传失败
44浏览 • 1回复 待解决
HarmonyOS如何实现图片上传
539浏览 • 1回复 待解决
HarmonyOS 上传图片问题
106浏览 • 1回复 待解决
HarmonyOS 上传图片失败
170浏览 • 1回复 待解决
HarmonyOS 上传图片异常
181浏览 • 1回复 待解决
配置文件文档问题有懂
3156浏览 • 1回复 待解决
HarmonyOS 如何上传文件
154浏览 • 1回复 待解决
HarmonyOS上传文件问题
617浏览 • 1回复 待解决