uni-app -云函数上传/删除

本文展示了如何在uni-app中使用云函数进行文件的上传和删除。通过uniCloud.deleteFile实现文件删除,并利用uni.chooseImage及uniCloud.uploadFile完成图片的选取与上传到云端,更新UI显示。

云函数上传/删除文件:

<template>
	<view class="content">
		<image class="logo" :src="bg_image_src"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
		<button @click="open">执行云函数</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				bg_image_src:''
			}
		},
		onLoad() {
			 
		},
		methods: {
				
			open(){
				let that = this
				//云函数删除 文件
				uniCloud.deleteFile({
					fileList:["882d21c4-b019-4fb7-94f3-37c5efb35ce3"],
					success(res) {
						console.log("删除成功",res) 
					}, 
					fail(res) {
						console.log("删除失败",res)
					}
				})
				//云函数添加 文件
				uni.chooseImage({
					count:1,
					success(res) {
						const tempFilePath = res.tempFilePaths[0] 
						console.log("获取图片成功-->",res,"地址",tempFilePath)
						uniCloud.uploadFile({
							filePath:tempFilePath,
							cloudPath: 'haha.png',
							fileType:'image',
							success(res) {
								console.log("上传成功",res)
								that.bg_image_src = res.fileID
							}, 
							fail(res) {
								console.log("上传失败",res)
							}
						})
					},
					fail(res) {
						console.log("获取图片失败",res)
					}
				})
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值