HarmonyOS 上传图片异常?

使用axios/原生http库,发请求上传参数位base64多图片参数(压缩后大小大概200k)会导致程序卡顿。

阅读 448
1 个回答

对base64格式的图片进行上传可以通过以下方式实现实现上传。

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-request-V5\#requestuploadfile9

https://gitee.com/openharmony-sig/ohos\_axios\#axios-%E5%AE%9E%E4%BE%8B

https://developer.huawei.com/consumer/cn/doc/harmonyos-references/remote-communication-rcp-V5\#section1188301010515

示例参考:

let context: Context = getContext(this);
const keyPemConent = context.resourceManager.getRawFileContentSync('testIMG.png')
let filesDir: string = context.filesDir
let filePath: string = filesDir + 'image1.png'

let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
fs.writeSync(file.fd, keyPemConent.buffer);
fs.fsyncSync(file.fd);
fs.closeSync(file);

let headers: rcp.RequestHeaders = {
  "accept": "multipart/form-data"
};

const multiForm1: Record<string, rcp.MultipartFormFieldValue | rcp.MultipartFormFieldValue[]> = {}
const multiForm: rcp.MultipartFormFields = {
  "testImg": {
    contentType: "image/png", // "text/plain"
    contentOrPath: filePath,
  },
}
const session = rcp.createSession();
let req = new rcp.Request("http://huawei.com", "POST", headers, multiForm);
// let req = new rcp.Request("http://huawei.com", "POST", headers, multiForm1);
session.fetch(req).then((response) => {
  console.info(`${response}`);
}).catch((err: BusinessError) => {
  console.error(`err: err code is ${err.code}, err message is ${err.message}`);
});
logo
HarmonyOS
子站问答
访问
宣传栏