chooseImage(){
uni.chooseImage({
count: 1,
sizeType: ['compressed'],
success: (res)=>{
console.log(JSON.stringify(res));
let fileName = "";
let filePath = "";
uni.showModal({
title:"图片选择成功",
content: JSON.stringify(res),
success: () => {},
fail: () => {},
});
if(res.tempFilePaths){
console.log(res.tempFilePaths[0]);
filePath = res.tempFilePaths[0];
fileName = filePath.substring(filePath.lastIndexOf("/")+1);
} else if(res.files){
console.log(res.files[0].path);
filePath = res.files[0].path;
fileName = filePath.substring(filePath.lastIndexOf("/")+1);
}
this.uploadFile(filePath, fileName);
},
fail:(err)=>{
console.log(JSON.stringify(err));
}
});
},
uploadFile(filePath, fileName){
uni.showModal({
title:"图片信息",
content: "路径:" + filePath + "===" + "名称:" + fileName,
success: () => {},
fail: () => {},
});
uni.showLoading({
title:"上传中..."
});
uni.uploadFile({
url: 'https://up-z0.qiniup.com',
header: {
"content-type":'multipart/form-data'
},
filePath: filePath,
fileType: 'image',
name: 'file',
timeout: 30000,
formData: {
'key': fileName,
'token': this.upLoadToken,
},
success: (result) => {
console.log("success=>"+JSON.stringify(result));
if(result.statusCode===200){
this.imageList.push(this.upLoadPath+"/"+(JSON.parse(result.data)).key);
if(this.imageList.length>=4&&this.imageList.indexOf("添加")!==-1){
this.imageList = this.imageList.filter(v=>{
return v!=="添加";
});
}
}
},
fail:(err)=>{
console.log("fail=>"+JSON.stringify(err));
},
complete:(data)=>{
uni.hideLoading();
console.log("complete=>"+JSON.stringify(data));
}
});
},

- 发布:2025-06-19 17:26
- 更新:2025-06-19 17:29
- 阅读:60
产品分类: uniapp/小程序/钉钉
PC开发环境操作系统: Mac
PC开发环境操作系统版本号: sonoma 14.7.4 (23H420)
HBuilderX类型: 正式
HBuilderX版本号: 4.66
第三方开发者工具版本号: 3.9.41 (3.9.41)
基础库版本号: vue 2
项目创建方式: HBuilderX
操作步骤:
预期结果:
chooseImage(){
uni.chooseImage({
count: 1,
sizeType: ['compressed'],
success: (res)=>{
console.log(JSON.stringify(res));
let fileName = "";
let filePath = "";
uni.showModal({
title:"图片选择成功",
content: JSON.stringify(res),
success: () => {},
fail: () => {},
});
if(res.tempFilePaths){
console.log(res.tempFilePaths[0]);
filePath = res.tempFilePaths[0];
fileName = filePath.substring(filePath.lastIndexOf("/")+1);
} else if(res.files){
console.log(res.files[0].path);
filePath = res.files[0].path;
fileName = filePath.substring(filePath.lastIndexOf("/")+1);
}
this.uploadFile(filePath, fileName);
},
fail:(err)=>{
console.log(JSON.stringify(err));
}
});
},
uploadFile(filePath, fileName){
uni.showModal({
title:"图片信息",
content: "路径:" + filePath + "===" + "名称:" + fileName,
success: () => {},
fail: () => {},
});
uni.showLoading({
title:"上传中..."
});
uni.uploadFile({
url: 'https://up-z0.qiniup.com',
header: {
"content-type":'multipart/form-data'
},
filePath: filePath,
fileType: 'image',
name: 'file',
timeout: 30000,
formData: {
'key': fileName,
'token': this.upLoadToken,
},
success: (result) => {
console.log("success=>"+JSON.stringify(result));
if(result.statusCode===200){
this.imageList.push(this.upLoadPath+"/"+(JSON.parse(result.data)).key);
if(this.imageList.length>=4&&this.imageList.indexOf("添加")!==-1){
this.imageList = this.imageList.filter(v=>{
return v!=="添加";
});
}
}
},
fail:(err)=>{
console.log("fail=>"+JSON.stringify(err));
},
complete:(data)=>{
uni.hideLoading();
console.log("complete=>"+JSON.stringify(data));
}
});
},
chooseImage(){
uni.chooseImage({
count: 1,
sizeType: ['compressed'],
success: (res)=>{
console.log(JSON.stringify(res));
let fileName = "";
let filePath = "";
uni.showModal({
title:"图片选择成功",
content: JSON.stringify(res),
success: () => {},
fail: () => {},
});
if(res.tempFilePaths){
console.log(res.tempFilePaths[0]);
filePath = res.tempFilePaths[0];
fileName = filePath.substring(filePath.lastIndexOf("/")+1);
} else if(res.files){
console.log(res.files[0].path);
filePath = res.files[0].path;
fileName = filePath.substring(filePath.lastIndexOf("/")+1);
}
this.uploadFile(filePath, fileName);
},
fail:(err)=>{
console.log(JSON.stringify(err));
}
});
},
uploadFile(filePath, fileName){
uni.showModal({
title:"图片信息",
content: "路径:" + filePath + "===" + "名称:" + fileName,
success: () => {},
fail: () => {},
});
uni.showLoading({
title:"上传中..."
});
uni.uploadFile({
url: 'https://up-z0.qiniup.com',
header: {
"content-type":'multipart/form-data'
},
filePath: filePath,
fileType: 'image',
name: 'file',
timeout: 30000,
formData: {
'key': fileName,
'token': this.upLoadToken,
},
success: (result) => {
console.log("success=>"+JSON.stringify(result));
if(result.statusCode===200){
this.imageList.push(this.upLoadPath+"/"+(JSON.parse(result.data)).key);
if(this.imageList.length>=4&&this.imageList.indexOf("添加")!==-1){
this.imageList = this.imageList.filter(v=>{
return v!=="添加";
});
}
}
},
fail:(err)=>{
console.log("fail=>"+JSON.stringify(err));
},
complete:(data)=>{
uni.hideLoading();
console.log("complete=>"+JSON.stringify(data));
}
});
},
实际结果:
chooseImage(){
uni.chooseImage({
count: 1,
sizeType: ['compressed'],
success: (res)=>{
console.log(JSON.stringify(res));
let fileName = "";
let filePath = "";
uni.showModal({
title:"图片选择成功",
content: JSON.stringify(res),
success: () => {},
fail: () => {},
});
if(res.tempFilePaths){
console.log(res.tempFilePaths[0]);
filePath = res.tempFilePaths[0];
fileName = filePath.substring(filePath.lastIndexOf("/")+1);
} else if(res.files){
console.log(res.files[0].path);
filePath = res.files[0].path;
fileName = filePath.substring(filePath.lastIndexOf("/")+1);
}
this.uploadFile(filePath, fileName);
},
fail:(err)=>{
console.log(JSON.stringify(err));
}
});
},
uploadFile(filePath, fileName){
uni.showModal({
title:"图片信息",
content: "路径:" + filePath + "===" + "名称:" + fileName,
success: () => {},
fail: () => {},
});
uni.showLoading({
title:"上传中..."
});
uni.uploadFile({
url: 'https://up-z0.qiniup.com',
header: {
"content-type":'multipart/form-data'
},
filePath: filePath,
fileType: 'image',
name: 'file',
timeout: 30000,
formData: {
'key': fileName,
'token': this.upLoadToken,
},
success: (result) => {
console.log("success=>"+JSON.stringify(result));
if(result.statusCode===200){
this.imageList.push(this.upLoadPath+"/"+(JSON.parse(result.data)).key);
if(this.imageList.length>=4&&this.imageList.indexOf("添加")!==-1){
this.imageList = this.imageList.filter(v=>{
return v!=="添加";
});
}
}
},
fail:(err)=>{
console.log("fail=>"+JSON.stringify(err));
},
complete:(data)=>{
uni.hideLoading();
console.log("complete=>"+JSON.stringify(data));
}
});
},
chooseImage(){
uni.chooseImage({
count: 1,
sizeType: ['compressed'],
success: (res)=>{
console.log(JSON.stringify(res));
let fileName = "";
let filePath = "";
uni.showModal({
title:"图片选择成功",
content: JSON.stringify(res),
success: () => {},
fail: () => {},
});
if(res.tempFilePaths){
console.log(res.tempFilePaths[0]);
filePath = res.tempFilePaths[0];
fileName = filePath.substring(filePath.lastIndexOf("/")+1);
} else if(res.files){
console.log(res.files[0].path);
filePath = res.files[0].path;
fileName = filePath.substring(filePath.lastIndexOf("/")+1);
}
this.uploadFile(filePath, fileName);
},
fail:(err)=>{
console.log(JSON.stringify(err));
}
});
},
uploadFile(filePath, fileName){
uni.showModal({
title:"图片信息",
content: "路径:" + filePath + "===" + "名称:" + fileName,
success: () => {},
fail: () => {},
});
uni.showLoading({
title:"上传中..."
});
uni.uploadFile({
url: 'https://up-z0.qiniup.com',
header: {
"content-type":'multipart/form-data'
},
filePath: filePath,
fileType: 'image',
name: 'file',
timeout: 30000,
formData: {
'key': fileName,
'token': this.upLoadToken,
},
success: (result) => {
console.log("success=>"+JSON.stringify(result));
if(result.statusCode===200){
this.imageList.push(this.upLoadPath+"/"+(JSON.parse(result.data)).key);
if(this.imageList.length>=4&&this.imageList.indexOf("添加")!==-1){
this.imageList = this.imageList.filter(v=>{
return v!=="添加";
});
}
}
},
fail:(err)=>{
console.log("fail=>"+JSON.stringify(err));
},
complete:(data)=>{
uni.hideLoading();
console.log("complete=>"+JSON.stringify(data));
}
});
},
bug描述:
uni-app uni.chooseImage方法实现后,调用uni.uploadFile方法,在鸿蒙next真机无反应,安卓真机和苹果真机正常;在鸿蒙next真机测试过程中uni.chooseImage方法实现返回函数,无tempFilePaths值或者tempFiles返回参数,安卓真机和苹果真机正常;
1***@qq.com (作者)
我先反馈一下,钉钉原生也是一样的情况
2025-06-19 17:30