uni-app 微信小程序,直传图片、视频到OSS
一:配置Bucket跨域访问
- 登录OSS管理控制台
- 单击Bucket列表,之后单击目标Bucket名称
- 单击权限管理 > 跨域设置,在跨域设置区域单击设置
- 单击创建规则,配置如下图所示
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7f4a83c4d683a38f98db8b389fa9af1c.png#pic_center)
二:微信公众号后台域名配置
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6692e1ee208843992a829dc2fb0523e4.png#pic_center)
三:配置密钥,上传
1、在 config.js文件中配置OSSAccessKeyId、AccessKeySecret和fileHost
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c95c437054b73ed64a78e1fcdb1d6385.png#pic_center)
2、页面引入
import uploadImage from '@/components/ossutil/uploadFile.js';
3、完整功能页面
<template>
<view style="padding-bottom: 10rpx;">
<view class="cu-bar bg-white margin-top">
<view class="action">
图片上传
</view>
<view class="action">
{{imgList.length}}
</view>
</view>
<view class="cu-form-group">
<view class="grid col-3 grid-square flex-sub">
<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
<image :src="imgList[index]" mode="aspectFill"></image>
<!-- @tap.stop 组织点击事件继续传播 -->
<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
<text class='cuIcon-close'></text>
</view>
</view>
<view class="solids" @tap="ChooseImage" v-if="imgList.length<6">
<text class='cuIcon-cameraadd'></text>
</view>
</view>
</view>
</view>
</template>
<script>
import uploadImage from '@/components/ossutil/uploadFile.js';
import main from '@/main.js'
export default {
data() {
return {
imgList: [],
submit_img:[],
oss_result:[]
}
},
onLoad:function(e){
},
methods:{
ChooseImage() {
uni.chooseImage({
count: 9,
sizeType: ['compressed'],
success: (res) => {
if (this.imgList.length != 0) {
this.imgList = this.imgList.concat(res.tempFilePaths);
this.submit_img.push(res.tempFilePaths);
}
else {
this.imgList = res.tempFilePaths
this.submit_img.push(res.tempFilePaths);
}
for (var i = 0; i < this.imgList.length; i++) {
uploadImage(
this.imgList[i],
'sd/suggest_img/jy/',
result => {
console.log("返回的图片路径++++"+result);
this.oss_result.push(result);
},
fail => {
console.log("fail+++++"+JSON.stringify(fail));
}
)
}
}
});
},
ViewImage(e) {
uni.previewImage({
urls: this.imgList,
current: e.currentTarget.dataset.url
});
},
DelImg(e) {
uni.showModal({
content: '确定要删除吗?',
cancelText: '取消',
confirmText: '确定',
success: res => {
if (res.confirm) {
this.imgList.splice(e.currentTarget.dataset.index, 1)
this.submit_img.splice(e.currentTarget.dataset.index, 1)
console.log("shengyu++++"+JSON.stringify(this.imgList));
}
}
})
}
}
}
</script>
<style>
</style>