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>

你可以使用 Element Plus 来实现在你的应用程序中上传图片的功能。下面是一个简单的示例代码: 首先,确保你已经正确导入了 Element Plus 组件: ```javascript import { ElUpload } from 'element-plus'; ``` 然后,在你的模板中使用 `ElUpload` 组件来创建一个上传图片的区域: ```html <template> <el-upload action="/your-upload-url" :on-success="handleSuccess" :before-upload="beforeUpload" :auto-upload="false" > <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过2MB</div> </el-upload> </template> ``` 在上述代码中,`action` 属性指定了上传图片的后端接口地址,你需要将它替换为你自己的地址。`on-success` 属性指定了上传成功时的回调函数,你可以在这里执行上传成功后的逻辑。`before-upload` 属性可以用来进行一些额外的上传前的验证,比如文件类型、大小等。`auto-upload` 属性设为 `false` 可以实现手动触发上传。 最后,在你的 Vue 组件中定义相应的方法: ```javascript methods: { handleSuccess(response, file, fileList) { console.log(response); // 在这里处理上传成功后的逻辑 }, beforeUpload(file) { const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'; const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG) { this.$message.error('只能上传jpg/png文件!'); } if (!isLt2M) { this.$message.error('文件大小不能超过2MB!'); } return isJPG && isLt2M; } } ``` 在 `handleSuccess` 方法中,你可以根据后端返回的响应进行进一步的处理,比如展示上传成功的图片或者更新数据。 以上是一个简单的示例,你可以根据自己的需求进行修改和扩展。希望能对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值