前端在uniapp中将图片上传到阿里云服务器

关于阿里云的配置就不说了(后台会配置好并在接口中返回给前端相关配置信息)

正常来说获取到上传图片信息,调用封装方法

async getSelectFileImg(val) {
  this.params.businessLicenseUrl = await getTempOssData(val);
},

封装方法,只不过这里是单张上传逻辑

其中先从后端获取到阿里云配置的所有信息,一同返给后端

// 单张图片批量上传到阿里OSS并返回临时链接
export async function getTempOssData(val) {
    console.log('上传的文件', val);
    const tempuuid = generateUUIDWithoutDashes()
    let otherUrl = `${tempuuid}.${val.tempFiles[0].extname}`
    const res = await uni.$http.get('/oss/getOssTokenByAccessID')
    const data = res.data.data;
    const {
        accessKeyId,
        dir,
        expiration,
        host,
        policy,
        signature
    } = data
    const uploadResult = await new Promise((resolve, reject) => {
        uni.uploadFile({
            url: host,
            filePath: val.tempFilePaths[0],
            name: 'file',
            formData: {
                key: `${dir}${otherUrl}`,
                OSSAccessKeyId: accessKeyId,
                policy,
                signature,
            },
            success: res => {
                console.log('OSS上传结果', res, tempuuid);
                if (res.statusCode == 204) {
                    console.log('临时链接', `${host}/${dir}${otherUrl}`);
                    resolve(`${host}/${dir}${otherUrl}`)
                } else {
                    uni.$showMsg('图片上传失败,请删除后重新上传')
                    reject(new Error('上传失败'));
                }
            },
            fail(err) {
                uni.$showMsg(err)
                reject(err);
            }
        })
    })
    return uploadResult
}

但是如果是说多张图片的话,使用遍历即可

uni.chooseMedia({
                mediaType: ['image', 'video'],
                sourceType: ['album', 'camera'],
                maxDuration: 30,
                camera: 'back',
                success: (val) => {
                    uni.$http.get('/oss/getOssTokenByAccessID').then((res) => {
                        const data = res.data.data;
                        const { accessKeyId, dir, expiration, host, policy, signature } = data;
                        for (let i = 0; i <= val.tempFiles.length - 1; i++) {
                            let type = val.tempFiles[i].tempFilePath.split('.');
                            let tempuuid = generateUUIDWithoutDashes();
                            uni.uploadFile({
                                url: host,
                                filePath: val.tempFiles[i].tempFilePath,
                                name: 'file',
                                formData: {
                                    key: `${dir}${tempuuid}.${type[type.length - 1]}`,
                                    OSSAccessKeyId: accessKeyId,
                                    policy,
                                    signature
                                },
                                success: (res) => {
                                    if (res.statusCode == 204) {
                                        this.params.url.push({ url: `${host}/${dir}${tempuuid}.${type[type.length - 1]}`, type });
                                    } else {
                                        uni.$showMsg('上传失败,请删除后重新上传');
                                    }
                                },
                                fail(err) {
                                    uni.$showMsg(err);
                                }
                            });
                        }
                    });
                }
            });

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

c__yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值