request.uploadFile(context, uploadConfig) 报错

request.uploadFile(context, uploadConfig)

uploadTask.on(“fail”,(taskStates)=>{

打印taskStates >>>{“path”:“tupian_out.jpg”,“responseCode”:5,“message”:“upload failed”}

HarmonyOS
2024-08-22 21:23:20
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

错误码为5,表示任务被主动暂停或停止。参考demo:

import request from '@ohos.request'; 
import { BusinessError } from '@ohos.base'; 
@Entry 
@Component 
struct Page { 
  @State message: string = 'Hello World'; 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
        Button('上传') 
          .onClick(() => { 
            let uploadTask: request.UploadTask; 
            let uploadConfig: request.UploadConfig = { 
              url: 'http://www.example.com', //需要手动替换为真实服务器地址  
              header: { 'Accept': '*/*' }, 
              method: "POST", 
              files: [{ 
                filename: "test", name: "test", 
                uri: "internal://cache/test.jpg", 
                type: "jpg" 
              }], 
              data: [{ name: "name123", value: "123" }], 
            }; 
            try { 
              request.uploadFile(getContext(), uploadConfig, (err: BusinessError, data: request.UploadTask) => { 
                let headerCallback = (headers: object) => { 
                  console.info("upOnHeader headers:" + JSON.stringify(headers)); 
                }; 
                uploadTask.on('headerReceive', headerCallback); 
                if (err) { 
                  console.error(`Failed to request the upload. Code: ${err.code}, message: ${err.message}`); 
                  return; 
                } 
                uploadTask = data; 
              }); 
            } catch (err) { 
              console.error(`Failed to request the upload. err: ${JSON.stringify(err)}`); 
            } 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}

上传请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-request-V5

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

分享
微博
QQ
微信
回复
2024-08-23 14:22:46
相关问题
HarmonyOS request.downloadFile报错
402浏览 • 1回复 待解决
: { request: WebResourceRequest}){} 报错
1632浏览 • 1回复 待解决
尝试获取Har context,app crash
1657浏览 • 1回复 待解决
是否支持Context跨线程传递
1728浏览 • 1回复 待解决
如何在UI中获取Context
2735浏览 • 1回复 待解决
Stage模型如何获取context?
3935浏览 • 1回复 待解决
HarmonyOS context.resourceDir问题
187浏览 • 1回复 待解决
如何获取FA模型下的Context
234浏览 • 1回复 待解决
如何在工具类中获取Context
1831浏览 • 1回复 待解决
worker.ts中如何获取context
1964浏览 • 2回复 待解决