选择图片后进行上传 报错:Failed to open/read local data from file/application
其中上传的代码:
/*
* post请求上传文件
* */
export function httpUploadFileForPost(url:string , fileUrl: string) : Promise<ResponseResult>{
// 每一个httpRequest对应一个HTTP请求任务,不可复用
let httpRequest = http.createHttp();
let userToken = LoginInfoTool.getInstance().userToken(false);
let response = httpRequest.request(
url,
{
method: http.RequestMethod.POST,
header: {
'Content-Type': 'multipart/form-data',
'token': userToken
},
multiFormDataList:[{
name: "harmonyOS_image.jpg",
contentType: "image/jpeg",
filePath:fileUrl
}],
usingCache: false,
connectTimeout: CommonValues.CONNECT_TIME_OUT,
readTimeout: CommonValues.CONNECT_TIME_OUT,
}
);
let serverData: ResponseResult = new ResponseResult();
Logger.info('PostUrl Request before response ', url);
return response.then((value: http.HttpResponse) => {
if(value.responseCode === CommonValues.HTTP_CODE_200){
Logger.info('PostUrl Request before response ', url);
serverData = value.result as ResponseResult;
Logger.info('PostUrl Response ', url + ' response: '+ JSON.stringify(serverData));
}
else{
serverData.msg = $r('app.string.http_error_message');
Logger.info('PostUrl Response', 'response: error');
}
httpRequest.destroy();
return serverData;
}).catch((err: Error) => {
httpRequest.destroy();
serverData.msg = $r('app.string.http_error_message');
Logger.info('PostUrl Response', 'response: error');
return serverData;
});
}
请参考下面从图库选择图片上传案例