goddess
侠客
侠客
  • 最后登录2021-12-20
  • 发帖数18
阅读:5724回复:1

uniapp使用uni.request上传图片失败

楼主#
更多 发布于:2020-08-10 16:43
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编辑了帖子]
doubleyong
管理员
管理员
  • 最后登录2025-12-02
  • 发帖数1198
  • 最爱沙发
  • 喜欢达人
  • 原创写手
  • 社区居民
  • 忠实会员
沙发#
发布于: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>
知识需要管理,知识需要分享
游客


返回顶部

公众号

公众号