云函数上传文件

<template>
	<view class="content">
		<button style="background: red;margin: 20px;" size="mini" type="primary" @click="callCloud">调用云函数</button>
         <image  style="width: 100%;height: 200px;":src="imgsrc" mode="aspectFit"  v-if="imgsrc"></image>
	</view>
</template>

<script>
	import {
    
    
		pathToBase64,
		base64ToPath
	} from '@/common/js/base64.js'
	export default {
    
    
		data() {
    
    
			return {
    
    
				title: 'Hello',
				imgsrc: '',
				resultInfo: {
    
    },
				percentCompleted:0
			}
		},
		onLoad() {
    
    

		},
		methods: {
    
    
            callCloud(){
    
    
				uniCloud.chooseAndUploadFile({
    
    
					type:"image",
					success:res=>{
    
    
						console.log(res,"chooseAndUploadFile");
					}
				})
			},
			callCloud2() {
    
    
				//前端代码
				let that=this;
				uni.chooseImage({
    
    
					count: 1,
					success(res) {
    
    
						console.log(res.tempFiles[0].name);
						if (res.tempFilePaths.length > 0) {
    
    
							let filePath = res.tempFilePaths[0]
				 		//进行上传操作 
						pathToBase64(filePath)
							.then(base64 => {
    
    
								base64ToPath(base64)
									.then(path => {
    
    
										that.imgsrc = path;
										console.log(path);
										console.log(that.imgsrc,'that.imgsrc')
									})
									.catch(error => {
    
    
										console.error(error)
									})
							})
							.catch(error => {
    
    
								console.error(error)
							})
						 
							// callback方式,与promise方式二选一即可
							uniCloud.uploadFile({
    
    
								filePath: filePath,
								cloudPath: res.tempFiles[0].name,
								onUploadProgress: function(progressEvent) {
    
    
									console.log(progressEvent,'progressEvent');
									var percentCompleted = Math.round(
										(progressEvent.loaded * 100) / progressEvent.total
									);
									that.percentCompleted=percentCompleted;
									console.log(percentCompleted,'percentCompleted');
								},
								success(suc) {
    
    
									console.log(suc,"suc");
									uniCloud.getTempFileURL({
    
    
										fileList:[suc.fileID],
										success:res2=>{
    
    
											console.log(res2,"getTempfileurl")
										}
									})
								},
								fail() {
    
    },
								complete() {
    
    }
							});

						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.car {
    
    
		font-size: 12px;
		width: 80%;
		margin: auto;
		box-sizing: border-box;
		box-shadow: 2px 2px 2px 2px rgba(0, 0, 0, 0.5);
		padding: 10px;
		border-radius: 2px;
		margin-top: 20px;
		margin-bottom: 40px;
		line-height: 25px;
		font-weight: bold;
		text-align: center;

		view {
    
    
			color: #333;

		}
	}

	.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>

猜你喜欢

转载自blog.csdn.net/weixin_45932157/article/details/123266100