HarmonyOS使用axios进行相册图片上传操作失败
使用axios进行图片上传失败 报错提示 Error: empty error message, 不知道是什么原因导致的。代码:
const photoViewPicker = new picker.PhotoViewPicker();
let ctx = getContext(this)
let photoSelectResult = await photoViewPicker.select(photoSelectOptions)
uris = photoSelectResult.photoUris;
for (let index = 0; index < uris.length; index++) {
const element = uris[index];
console.error('====>uri is:' + element + ', start read file')
fs.open(element, (err, file) => {
if (err) {
console.error("====>file open failed with error message: " + err.message + ", error code: " +
err.code);
} else {
let filepath = `${ctx.cacheDir}/${file.name}`;
console.error('====>filepath is:' + filepath + ', start copy file')
fs.copyFile(file.fd, filepath, () => {
// 上传的uri
let uri = fileUri.getUriFromPath(filepath)
console.error("文件uri路径:" + uri)
let realUri = `internal://cache/${file.name}`;
console.error("real uri:" + realUri)
// 执行上传
let request: AxiosRequestConfig<FormData> = {
url: config.subUrl,
headers: {
'Content-Type': 'multipart/form-data'
},
context: getContext(),
method: "post",
params: params,
data: formData
}
let resp: AxiosResponse<HttpResponseModel> =
await axios.request<HttpResponseModel, AxiosResponse<HttpResponseModel>, FormData>(request)
});
}
})
}
console.error('photoViewPicker.select to file succeed and uris are:' + uris);
} catch (err) {
console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
}
错误提示:
E [ecmascript] Pending exception before ToObject called in line:239, exception details as follows:
Error: empty error message
at upload (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/upload.js:140:1)
at anonymous (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/index.js:31:1)
at anonymous (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/index.js:26:1)
at dispatchRequest (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/core/dispatchRequest.js:51:1)
at request (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/core/Axios.js:170:1)
at wrap (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/helpers/bind.js:5:1)
at apiRequest (entry/src/main/ets/core/network/service.ets:90:15)
[ecmascript] Pending exception before Has called in line:1891, exception details as follows:
Error: empty error message
at upload (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/upload.js:140:1)
at anonymous (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/index.js:31:1)
at anonymous (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/index.js:26:1)
at dispatchRequest (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/core/dispatchRequest.js:51:1)
at request (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/core/Axios.js:170:1)
at wrap (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/helpers/bind.js:5:1)
at apiRequest (entry/src/main/ets/core/network/service.ets:90:15)
[ecmascript] Pending exception before IsMixedDebugEnabled called in line:3573, exception details as follows:
Error: empty error message
at upload (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/upload.js:140:1)
at anonymous (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/index.js:31:1)
at anonymous (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/index.js:26:1)
at dispatchRequest (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/core/dispatchRequest.js:51:1)
at request (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/core/Axios.js:170:1)
at wrap (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/helpers/bind.js:5:1)
at apiRequest (entry/src/main/ets/core/network/service.ets:90:15)
[default] ConstructorOptimize:2520 occur exception need return
HarmonyOS
赞
收藏 0
回答 1
待解决
相关问题
rcp上传的时候如果是相册里面图片路径怎么上传,直接上传uri会失败
268浏览 • 1回复 待解决
HarmonyOS如何上传图片到模拟器相册
654浏览 • 1回复 待解决
拉起相册选择图片页面失败
1729浏览 • 1回复 待解决
HarmonyOS版Axios走文件上传请求时,如果ssl握手失败,没有回调
350浏览 • 1回复 待解决
通过web上传图片并进行预览
802浏览 • 1回复 待解决
基于PhotoViewPicker对图片进行操作
625浏览 • 1回复 待解决
HarmonyOS调用相册管理api,创建并保存图片至相册失败,查看图片是空白
280浏览 • 1回复 待解决
使用FilePick上传uri失败,报错201
1628浏览 • 1回复 待解决
如何对相册图片进行编辑裁剪
1646浏览 • 1回复 待解决
HarmonyOS 测试命令行相关使用
305浏览 • 1回复 待解决
从相册选择图片上传到端上,图片较大需要先压缩在上传到端上,需要图片压缩工具
1603浏览 • 1回复 待解决
从本机获取图片进行上传到spring后端
2259浏览 • 1回复 待解决
HarmonyOS图片上传相关问题
323浏览 • 1回复 待解决
图片剪切,HarmonyOS提供图片剪切能力,并对截取后的资源进行操作。
930浏览 • 1回复 待解决
如何使用onShowFileSelector()函数拉起图库上传图片
2096浏览 • 1回复 待解决
HarmonyOS如何向模拟器相册添加一些图片?如何实现通过接口把从相册选的图片上传到服务器?
326浏览 • 1回复 待解决
HarmonyOS web上传文件和图片
106浏览 • 1回复 待解决
HarmonyOS如何实现图片的上传
288浏览 • 1回复 待解决
HarmonyOS图片保存相册问题
286浏览 • 1回复 待解决
axios网络功能问题,axios是否会支持网络连接失败的重试机制?
885浏览 • 1回复 待解决
HarmonyOS Web 图片上传功能失效
49浏览 • 1回复 待解决
HarmonyOS 相册保存图片设置title报错
243浏览 • 1回复 待解决
HarmonyOS 使用@ohos.request 上传图片后,获取不到服务器返回
415浏览 • 1回复 待解决
怎么实现后台上传图片?
2249浏览 • 1回复 待解决
HarmonyOS DevEco Testing平台上传文件提示安装失败
373浏览 • 1回复 待解决
在请求中未设置请求头,headers: { ‘Content-Type’: ‘multipart/form-data’ },请参考gitee的代码实例:https://gitee.com/openharmony-sig/ohos_axios#%E5%BD%93%E4%B8%8A%E4%BC%A0%E7%9A%84%E5%86%85%E5%AE%B9%E4%B8%BAarraybuffer%E6%97%B6%E7%94%A8%E6%B3%95%E5%A6%82%E4%B8%8B
formData里有其他字段会触发empty error message问题,只包含file这一个字段可以请求成功: