HarmonyOS request.uploadFile 上传文件成功,但是并无服务器数据返回

在使用request.uploadFile 上传相册选中的图片时,进行了沙箱处理,上传看似也成功了,但是没有我方服务器的返回数据。代码:

async upFile(uri: Array<string>) { 
  JLog.d("原图:::" + uri[0]) 
  let header = new Map<Object, string>(); 
  header.set('Content-Type', 'multipart/form-data'); 
  let segments = uri[0].split('/') 
  let fileName = segments[segments.length-1] 
  let realUri = this.context.cacheDir + "/" + fileName 
  JLog.d("沙箱图处理:::realUri:" + realUri) 
  try { 
    let file =  fs.openSync(uri[0]) 
    fs.copyFileSync(file.fd, realUri) 
  } catch (err) { 
    JLog.e('err.code : ' + err.code + ', err.message : ' + err.message) 
  } 
  let files: Array<request.File> = [{ filename: fileName, name: "file", uri: `internal://cache/${fileName}`, type: 'jpg' }] 
  let data: Array<request.RequestData> = [{ name: 'TenantID', value: 'ysz' },{ name: 'file', value: 'files' }]; 
  let uploadConfig: request.UploadConfig = { 
    url: ApiUrl.UPLOAD_IMAGE_URL, 
    header: header, 
    method: 'POST', 
    files: files, 
    data: data 
  } 
  try { 
    let context = getContext(this) as common.UIAbilityContext; 
    request.uploadFile(context, uploadConfig) 
      .then((uploadTask: request.UploadTask) => { 
        uploadTask.on('complete', (taskStates: Array<request.TaskState>) => { 
          for (let i = 0; i < taskStates.length; i++) { 
            JLog.d(`upload complete taskState: ${JSON.stringify(taskStates[i])}`); 
          } 
        }); 
        uploadTask.on("progress", (size, tot) => { 
          JLog.d("进度" + size + '/' + tot) 
        }) 
      }) 
      .catch((err: BusinessError) => { 
        JLog.e(`Invoke uploadFile failed, code is ${err.code}, message is ${err.message}`); 
      }) 
  } catch (error) { 
    let err: BusinessError = error as BusinessError; 
    JLog.e(`Invoke uploadFile failed, code is ${err.code}, message is ${err.message}`); 
  } 
}

打印部分日志:

原图:::file://media/Photo/8/IMG_1715853988_003/IMG_20240516_180448.jpg 
沙箱图处理:::realUri:/data/storage/el2/base/haps/entry/cache/IMG_20240516_180448.jpg 
进度3647696/3647696 
upload complete taskState: {“path”:"/data/storage/el2/base/haps/entry/cache/IMG_20240516_180448.jpg",“responseCode”:0,“message”:""}
HarmonyOS
2024-09-06 11:03:23
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

可以参考开启on(‘headerReceive’),其中响应体的 header 和 body 都会在这个 header object 里存放https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-request-V5#onheaderreceive7

分享
微博
QQ
微信
回复
2024-09-06 15:44:03
相关问题
HarmonyOS 文件上传服务器问题
270浏览 • 1回复 待解决
request.uploadFile(context, uploadConfig) 报错
377浏览 • 1回复 待解决
HarmonyOS Rcp.post上传图库文件服务器
136浏览 • 1回复 待解决
request上传返回错误码401
104浏览 • 2回复 待解决
上传文件后,如何接受后端返回数据
1836浏览 • 1回复 待解决
centos服务器系统宕机
2006浏览 • 1回复 待解决
服务器ECS自带MySQL数据库吗?
2950浏览 • 1回复 待解决
HarmonyOS HTTPS请求如何验证服务器证书
490浏览 • 1回复 待解决
mysql shell 如何查看服务器状态?
3979浏览 • 1回复 待解决
服务器的 mysql 密码问题
2250浏览 • 0回复 待解决