|
阅读:5724回复:1
uniapp使用uni.request上传图片失败pushImage() {
uni.chooseImage({
count: 6, //默认9
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album'], //从相册选择
success: (res) => {
this.imgFile.push(res.tempFiles[0])
this.imageHead = res.tempFilePaths[0]
this.sendImage(res.tempFilePaths)
}
});
},获取图片信息,this.sendImage(res.tempFilePaths)是个函数,传参数调用sendImage(option) {
uni.showLoading({
title: '上传中'
});
uni.uploadFile({
url: API.fileupload,
filePath: option[0],
name: 'file',
formData:{
token:this.token
},
success: (msg) => {
console.log(msg);
uni.hideLoading()
}
});
}, [goddess于2020-08-10 18:25编辑了帖子]
|
|
|
沙发#
发布于:2022-07-21 16:32
vue 3 版本
<script>
import {getCurrentInstance} from 'vue'
setup(){
const { proxy } = getCurrentInstance(); // proxy 替代this
let token = uni.getStorageSync('token');
const pushImage = () => {
uni.chooseImage({
count: 6, //默认9
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album'], //从相册选择
success: (res) => {
// this.imgFile.push(res.tempFiles[0])
// this.imageHead = res.tempFilePaths[0]
// 要保存用vue3的reactive, 不存在就注释掉
sendImage(res.tempFilePaths)
}
});
}
// 获取图片信息,this.sendImage(res.tempFilePaths)是个函数,传参数调用
const sendImage= (option) => {
uni.showLoading({
title: '上传中'
});
uni.uploadFile({
url: API.fileupload,
filePath: option[0],
name: 'file',
formData:{ token },
success: (msg) => {
console.log(msg);
uni.hideLoading()
}
});
}
}
</script> |
|
|