上传成功会返回上传成功的进度,服务器接收到数据后需要把数据写入数据库,
监听到上传成功进度以后重新请求一下接口,获取一下新的数据
也可以通过下面方法获取 HarmonyOS上传图片成功后返回的数据
request.uploadFile(getContext(), uploadConfig).then((data: request.UploadTask) => {
uploadTask = data;
//监听上传进度
uploadTask.on("progress", (uploadedSize, totalSize) => {
console.log("upload totalSize:" + totalSize + " uploadedSize:" + uploadedSize)
})
//获取服务器返回的接口数据
uploadTask.on('headerReceive', (headers) => {
console.log(headers["body"]) //{"success":"true","path":"public/upload/h0wncqAQLRj6wcMLcQnC8HFE.jpg"}
});
//通过拼接域名可以访问服务器返回的图片地址 https://xiaomi.itying.com/public/upload/h0wncqAQLRj6wcMLcQnC8HFE.jpg
}).catch((err: BusinessError) => {
console.error(`Failed to request the upload. Code: ${err.code}, message: ${err.message}`);
});