element-plus el-upload 自定义缩略图

先看效果:

    鼠标未经过时:                                     鼠标经过时:

<template>
	<div class="container">
		<el-upload v-model:file-list="fileLists" multiple :http-request="httpRequest" list-type="picture-card">
			<el-link type="primary">选择附件</el-link>
			<template #file="{ file }">
				<div>
					<img class="el-upload-list__item-thumbnail" src="../../../../assets/images/excel.jpg" alt="" />
					<div style="margin-top: -18px; font-size: 12px; text-align: center; overflow: hidden">{{ file.name }}</div>
					<span class="el-upload-list__item-actions">
						<span class="el-upload-list__item-delete" @click="handleDownload(file)">
							<el-icon><Download /></el-icon>
						</span>
						<span class="el-upload-list__item-delete" @click="handleRemove(file)">
							<el-icon><Delete /></el-icon>
						</span>
					</span>
				</div>
			</template>
		</el-upload>
	</div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';

//文件列表
const fileLists = ref([]);

//根据id获取附件并处理数据
const GetAttachmentById = async (id:any) => {
	const res = await api(id);
	let fileList: any = [];
	res.data.map((item: any) => {
		let newItem: any = {};
		newItem.id = item.Id;
		newItem.name = item.Name;
		newItem.url = item.URL;
		fileList.push(newItem);
	});
	fileLists.value = fileList;
};

//上传附件
const httpRequest = async (data: any) => {
	const formData = new FormData();
	formData.append('File', data.file);
	const res = await api(formData );
	ElMessage.success('上传成功');
};

//移除附件
const handleRemove = (file: any) => {
	ElMessageBox.confirm(`您确认删除 "${file.name}" 吗?如果删除则数据不可恢复,请谨慎操作!`, '信息', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			await api(file.id)
			ElMessage.success('删除成功');
		})
		.catch(() => {});
};

//下载已上传附件
const handleDownload = async (file: any) => {

};
</script>

<style lang="scss" scoped>
:deep(.el-upload-list) {
	flex-direction: row-reverse !important;
	.el-upload--picture-card {
		width: 56px;
		height: 24px;
		margin-right: 20px;
		border: none;
		background-color: #fff;
	}
	.el-upload-list__item {
		width: 101px;
		height: 104px;
		position: relative;
		border: none;
		.el-upload-list__item-actions {
			span {
				margin: 0 9px !important;
			}
		}
	}
}
</style>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 element-plus 提供的 Upload 组件来实现上传图片文件的功能。首先,你需要在项目中安装 element-plus: ``` npm install element-plus --save ``` 然后,在你的代码中引入需要的组件和样式: ```javascript import { createApp } from 'vue'; import ElementPlus from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; const app = createApp(App); app.use(ElementPlus); app.mount('#app'); ``` 接下来,你可以在你的模板中使用 Upload 组件来实现上传图片文件的功能,例如: ```html <template> <el-upload class="upload-demo" action="/your-upload-url" :auto-upload="false" :on-change="handleChange" > <el-button slot="trigger" size="small" type="primary">点击上传</el-button> <div slot="tip" class="upload-demo-tip">只能上传jpg/png文件,且不超过2MB</div> </el-upload> </template> <script> export default { methods: { handleChange(file) { // 处理文件变化事件 console.log(file); } } }; </script> ``` 在上面的代码中,你需要根据实际情况修改 `action` 属性的值为你的上传接口地址。`auto-upload` 属性设置为 `false` 表示手动触发上传,你可以根据需求设置为 `true` 来自动上传。 另外,在 `handleChange` 方法中,你可以处理文件变化事件,例如打印上传的文件信息、调用接口进行文件上传等。 以上是使用 element-plus 实现上传图片文件的基本步骤,你可以根据自己的需求进行更详细的配置和处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值