element plus el-upload 和el-image 配合图片上传和显示

我在用 element plus的el-upload组件上传1个图片,el-image组件显示图片的过程中,碰到了图片上传第二次时,无法执行上传替换前一个文件,然后又碰到无法执行 http-request 的问题。具体解决代码如下。

2个组件的定义代码如下:

<el-col :xs="24" :sm="12" :md="12" :lg="24" :xl="24" class="mb20">
					   <el-form-item label="参考图" prop="image">
							<el-upload 
							ref="myupload"
							:show-file-list="true" 
							:http-request="uploadAttachmentHandle" 
							:limit="1" 
							:on-exceed="handleExceed"	
							:auto-upload="true"						 
							>
								<el-button type="primary">上传</el-button>
							</el-upload>
							<el-image
							 
						    v-model="imageContainer.imgeurl" 
							style="width: 100px; height: 100px"
							:src="imageContainer.imgeurl"
							:zoom-rate="1.2"
							:max-scale="7"
							:min-scale="0.2"
							:preview-src-list="imageContainer.imgeurl"
							show-progress
							:initial-index="4"
							fit="cover"
							/>
							<!-- :preview-src-list="imgeurl" -->
						</el-form-item>
						
</el-col>

业务函数部分如下:

import { UploadRequestOptions,genFileId } from "element-plus";  
import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus';


//文件上传替换前一个文件
//:limit="1" 属性和 :on-exceed="handleExceed" 配合使用 ,每次替换旧图。 注意limit是1
const myupload = ref<UploadInstance>();  //myupload 必须和 el-upload 组件的ref属性名同名
const handleExceed: UploadProps['onExceed'] = (files) => {
	//下面这两行不能在这里执行,执行了就会影响 :http-request="uploadAttachmentHandle" 的接口请 
    //求执行。要放到下面的函数中最后执行
	// if(myupload.value!=undefined) 
	// 	myupload.value!.clearFiles(); 
	const file = files[0] as UploadRawFile;
	file.uid = genFileId();
	myupload.value!.handleStart(file); 
 
}



const uploadAttachmentHandle = async (options: UploadRequestOptions) => {
	 
	const res = await cbbjMaterialApi.uploadAttachment(options);
	state.ruleForm.image = res.data.result?.id;
	imageContainer.imgeurl=res.data.result?.url;
	if(myupload.value!=undefined) 
		myupload.value!.clearFiles(); 
};

el-upload 插件可以直接通过style样式调整 文件列表宽度

要控制上传文件类型,可以使用el-upload的file-list属性,它是一个数组,用于存储已上传的文件列表。通过监听before-upload事件,可以在上传之前对文件进行检查筛选。 示例代码: ```html <template> <el-upload class="upload-demo" action="/upload" :file-list="fileList" :before-upload="beforeUpload" :on-success="onSuccess" :on-error="onError" :auto-upload="false"> <el-button slot="trigger" size="small" type="primary">选取文件</el-button> <el-button style="margin-left: 10px;" size="small" type="success" @click="uploadFiles">上传到服务器</el-button> </el-upload> </template> <script> export default { data() { return { fileList: [] }; }, methods: { beforeUpload(file) { // 判断文件类型是否符合要求 const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'; const isLt2M = file.size / 1024 / 1024 < 2; if (!isJpgOrPng) { this.$message.error('上传头像图片只能是 JPG/PNG 格式!'); return false; } if (!isLt2M) { this.$message.error('上传头像图片大小不能超过 2MB!'); return false; } // 添加到fileList中 this.fileList.push(file); return false; }, onSuccess() { this.$message.success('上传成功'); }, onError() { this.$message.error('上传失败'); }, uploadFiles() { // 执行上传操作 this.$refs.upload.submit(); } } }; </script> ``` 在before-upload事件中,我们检查了文件类型大小,如果符合要求,则将文件添加到fileList中。在上传之前,我们可以通过遍历fileList来控制上传的文件类型。 另外,我们将auto-upload属性设置为false,这样选择文件后不会自动上传,而是需要手动调用submit方法上传文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值