uni-app 微信小程序,直传图片、视频到OSS

一:配置Bucket跨域访问

  1. 登录OSS管理控制台
  2. 单击Bucket列表,之后单击目标Bucket名称
  3. 单击权限管理 > 跨域设置,在跨域设置区域单击设置
  4. 单击创建规则,配置如下图所示
    在这里插入图片描述

二:微信公众号后台域名配置

在这里插入图片描述

三:配置密钥,上传

1、在 config.js文件中配置OSSAccessKeyId、AccessKeySecret和fileHost

在这里插入图片描述

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, //默认9
					sizeType: ['compressed'], //compressed压缩图,original
					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>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值