HarmonyOS 分片上传对后台接口有什么要求

下方代码中设置了config的begins、ends,后台接口需要做什么配置吗

let filePath: string = context.cacheDir + "/" + "01.mp3"
// let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_WRITE)
let name: string = file.name.substring(0, file.name.lastIndexOf("."))
// let urlPath: string = "http://127.0.0.1"
let attachments: Array<request.agent.FormItem> = [{
  name: name,
  value: {
    filename: file.name,
    mimeType: "application/octet-stream",
    path: filePath,
  }
}];
let config: request.agent.Config = {
  action: request.agent.Action.UPLOAD,
  url: urlPath, // 需要手动将 url 替换为真实服务器的 HTTP 协议地址
  title: 'taskOnTest',
  description: 'Sample code for event listening',
  mode: request.agent.Mode.FOREGROUND,
  overwrite: false,
  method: "PUT",
  data: attachments,
  saveas: "./",
  network: request.agent.Network.CELLULAR,
  metered: false,
  roaming: true,
  retry: true,
  redirect: true,
  index: index,
  begins: beginPoint,
  ends: endPoint,
  gauge: false,
  precise: false,
  // token: "it is a secret",
  headers:header
};
let createOnCallback = (progress: request.agent.Progress) => {
  console.info('upload task progress.' + progress.index);
};

request.agent.create(context, config).then((task: request.agent.Task) => {
  task.on('progress', createOnCallback);
  console.info(`Succeeded in creating a upload task. result: ${task.tid}`);
  task.start();
}).catch((err: BusinessError) => {
  console.error(`Failed to create a upload task, Code: ${err.code}, message: ${err.message}`);
});
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

通过“上传下载”接口的config来配置,使用begins 和 ends 能实现片段上传,对应的文件拼接逻辑需要自己实现。

参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-request-V5#config10

分片上传可以参考 on(‘progress’) 下的示例代码来实现: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-request-V5#onprogress10

分片的示例代码大体结构如下:

import fs from '@ohos.file.fs';
import { BusinessError, request } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';

let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext
let filePath: string = context.cacheDir + "/" + "01.mp3"
let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_WRITE)
let name: string = file.name.substring(0, file.name.lastIndexOf("."))
let urlPath: string = "http://127.0.0.1"
let attachments: Array<request.agent.FormItem> = [{
  name: name,
  value: { filename: file.name, mimeType: "application/octet-stream", path: filePath, }
}];
let config: request.agent.Config = {
  action: request.agent.Action.UPLOAD,
  url: urlPath,
  // 需要手动将 url 替换为真实服务器的 HTTP 协议地址
  title: 'taskOnTest',
  description: 'Sample code for event listening',
  mode: request.agent.Mode.FOREGROUND,
  overwrite: false,
  method: "PUT",
  data: attachments,
  saveas: "./",
  network: request.agent.Network.CELLULAR,
  metered: false,
  roaming: true,
  retry: true,
  redirect: true,
  index: 0,
  begins: 0,
  ends: -1,
  gauge: false,
  precise: false,
  token: "it is a secret"
};
let createOnCallback = (progress: request.agent.Progress) => {
  console.info('upload task progress.');
};
request.agent.create(context, config).then((task: request.agent.Task) => {
  task.on('progress', createOnCallback);
  console.info(`Succeeded in creating a upload task. result: ${task.tid}`);
  task.start();
}).catch((err: BusinessError) => {
  console.error(`Failed to create a upload task, Code: ${err.code}, message: ${err.message}`);
});
分享
微博
QQ
微信
回复
1天前
相关问题
MongoDB中的分片什么意思?
3310浏览 • 1回复 待解决
怎么实现后台上传图片?
2360浏览 • 1回复 待解决
harmonyOS对芯片是否要求
9243浏览 • 2回复 已解决
HarmonyOS 文件上传接口数据返回信息
63浏览 • 1回复 待解决
请问下HarmonyOS怎么把文件分片
568浏览 • 1回复 待解决
是否符合要求的半弹窗组件
1974浏览 • 1回复 待解决