Element 手动上传图片

<template>
	<el-card>
		<el-form>
			<el-form-item prop="logo" label="云店LOGO:">
				<el-upload action="#" :auto-upload="false" :on-change="beforeUpload" :show-file-list="false"
					accept=".BMP,.JPG,.JPEG,.PNG,.GIF">
					<i v-if='!body.logo' class="img-log logo-empty"></i>
					<img v-else class="img-log" :src="body.logo" />
				</el-upload>
			</el-form-item>
			<el-form-item>
				<el-button @click="uploadFile">上传</el-button>
			</el-form-item>
		</el-form>
	</el-card>
</template>

<script setup>
	import { reactive } from "vue"	
	let fileTemp = undefined;
	const body = reactive({ logo: "" })

	function beforeUpload(file) {
		fileTemp = file.raw
		const fileReader = new FileReader();
		fileReader.readAsDataURL(file.raw);
		fileReader.onload = () => {
			body.logo = fileReader.result;
		};
	}

	function uploadFile() {
		let params = { type: 1, source: 1, image: fileTemp, }
		let url = '/basic/v3/upload/index'
		this.$http.post(url, params, that.$demain.base).then(resp => {
			if (resp.code === 200) {
				body.logo = resp.data.url;
				fileTemp = undefined;
			}
		})
	}
</script>

<style lang="scss" scoped>
	@import '@/assets/style/common.scss';

	.el-card {
		height: 100%;

		.img-log {
			width: $px88;
			height: $px88;
			border: $px1 dotted $border-color;
			border-radius: $px4;
		}

		.logo-empty {
			position: relative;

			&::before {
				position: absolute;
				top: calc(50% - $px2);
				left: calc(50% - $px20);
				display: block;
				content: "";
				height: $px4;
				width: $px40;
				background-color: $placeholder;
			}

			&::after {
				position: absolute;
				top: calc(50% - $px20);
				right: calc(50% - $px2);
				display: block;
				content: "";
				height: $px40;
				width: $px4;
				background-color: $placeholder;
			}

			&:hover {
				background-color: #C9C9C9;

				&::before {
					background-color: #fff;
				}

				&::after {
					background-color: #fff;
				}
			}
		}
	}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值