首页 > 其他分享 >antdvue 上传图片最多四张,有预览删除

antdvue 上传图片最多四张,有预览删除

时间:2023-01-30 20:22:16浏览次数:34  
标签:antdvue false 预览 form res 100px 四张 file reader

<template>
<div ref="classReview">
<a-modal
:body-style="modalStyle"
:confirm-loading="confirmLoading"
:footer="null"
:getContainer="()=>$refs.classReview"
:visible="isVisible"
:width="600"
title="点评课程"
@cancel="handleCancel"
>
<div class="modalBox">
<div class="infoBox flex flex-sb">
<span>学期</span>
<span>教学周</span>
<span>课程</span>
<span>老师</span>
</div>
<div class="formBox">
<a-form-model
ref="ruleForm"
:model="form"
:rules="rules"
layout="vertical"
>
<a-form-model-item label="评价得分(满分100)" prop="score">
<a-input v-model="form.score"></a-input>

</a-form-model-item>
<a-form-model-item label="上传照片(最多4张)" prop="urlList">
<input
id="uplode"
ref="imgFile"
accept=".jpeg,.png,.gif,.jpg"
hidden
multiple="multiple"
type="file"
@change="uploadFile($event)"
/>
<!-- 图片预览-->
<div class="imgsBox flex">
<div v-for="(urlImg,uIndex) in form.urlList" class="imgBox">
<img :src="urlImg">
<div class="previewIcon">
<a-icon :style="{fontSize:'20px',marginRight:'16px'}" type="eye" @click="previewImg(urlImg)"/>
<a-icon :style="{fontSize:'20px'}" type="delete" @click="delImg(uIndex)"/>
</div>
</div>

<div v-if="form.urlList.length<4" class="uploadBox" @click="$refs.imgFile.click()">
<a-icon type="plus"/>
<span>文件上传</span>

<a-modal :footer="null" :visible="previewVisible" @cancel="previewVisible=false">
<img :src="previewImage" alt="example" style="width: 100%"/>
</a-modal>
</div>
</div>

</a-form-model-item>
<a-form-model-item label="意见与建议(200字内)" prop="advice">
<a-input v-model="form.advice" :auto-size="{ minRows: 3, maxRows: 10 }" :maxLength="200"
type="textarea"></a-input>

</a-form-model-item>

<a-form-model-item :wrapper-col="{ span: 8, offset: 16 }">

<span class="btn_default" style="margin-left: 10px;" @click="handleCancel">
取消
</span>

<span class="btn_primary" @click="submitForm">
提交
</span>
</a-form-model-item>
</a-form-model>


</div>
</div>
</a-modal>
</div>

</template>

<script>
import Cookies from "js-cookie";

const modalStyle = {
height: '500px',
width: '600px',
overflowY: 'scroll'
}

function getBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
});
}

export default {
components: {},
props: {
isShowDialog: {
type: Boolean,
default: false
},
courseId: {
type: String
},

},
data() {

return {
modalStyle,
confirmLoading: false,
previewVisible: false,
previewImage: '',
labelCol: {span: 8},
wrapperCol: {span: 10},
other: '',
form: {
score: 0,
urlList: [],
advice: ''

},
rules: {
score: [{
required: true,
pattern:/^100$|^(\d|[1-9]\d)(\.\d{1,4})*$/,
message: '请输入1-100数字,最多可保留四位小数',
trigger: 'change'
},],
advice: [{required: true, message: '请填写问卷须知', trigger: ['blur', 'change']}],


},
}
},
computed: {
isVisible: {
get() {
return this.isShowDialog
},
set(val) {
this.$emit('update:isShowDialog', val)
}
}
},
mounted() {
},
methods: {

uploadFile(e) {
let files = e.target.files;
let file = files[0];
let formData = new FormData();
formData.append('file', file);
console.log("文件:", formData);
api.uploadFile(formData).then((res) => {
if (res.code === 200) {
this.form.urlList.push(res.data.HttpUrl)
}
})
},
previewImg(url) {
this.previewImage = url
this.previewVisible = true;
},
delImg(index) {
this.form.urlList.splice(index, 1)
},
handleCancel(e) {
this.$emit('update:isShowDialog', false)
},
submitForm() {
this.$refs.ruleForm.validate(valid => {
if (valid) {
console.log('submit!', this.form);
this.$emit('updateReviews',this.form)
this.$emit('update:isShowDialog',false)
// api.saveQInfo(this.form).then(res => {
// if (res.code == 200) {
// this.$message.success('提交成功!')
// this.questionnaireId = res.data.questionnaireId
// let temp = {
// "copyQuestionnaireId": this.questionnaireId,
// "questionnaireId": this.oldQuestionnaireId
// }
// api.copyQuestion(temp).then(res => {
//
// })
// this.current++
// }
// })
} else {
console.log('error submit!!', this.form);
return false;
}
});

},

}

}
</script>

<style lang="less" scoped>
@import "@/style/variables.less";

/deep/ .ant-modal-header {
border-bottom: none;
}

.formBox {
.imgsBox {
.imgBox {
position: relative;
margin-right: 12px;

img {
width: 100px;
height: 100px;
}

.previewIcon {
position: absolute;
top: 0;
left: 0;
width: 100px;
height: 100px;
text-align: center;
line-height: 100px;
color: transparent;

&:hover {
display: block;
color: #fff;
background-color: rgba(255, 255, 255, .5);
}
}


}

.uploadBox {
width: 100px;
height: 100px;
border: 1px solid #e7e7e7;
color: #999;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
}


}
</style>

标签:antdvue,false,预览,form,res,100px,四张,file,reader
From: https://www.cnblogs.com/connie256/p/17077168.html

相关文章

  • 前端如何在上传图片实现图片预览
    <!DOCTYPEhtml><html> <head> <metacharset="utf-8"/> <title></title> </head> <body> <imgsrc=""alt=""> <inputtype="file"> </body> &l......
  • U3D编辑器开发&粒子特效/动画预览器示例
    概述U3D提供了一套拓展编辑器的接口,可以用于直接在编辑器非播放模式运行程序。常用于运行一些工具程序,例如资源管理。在做技能编辑器等工具程序时,也可以使用运行模式接口......
  • windows11预览版装WSA心得
    windows11预览版装WSA心得这两天心血来潮想要装个WSA(安卓windows子系统),原来一直用的安卓模拟器(mumu啊蓝叠啊逍遥啊),但感觉像wsa这种安卓系统与主系统融合的模式更带感,于......
  • windows11预览版装WSA心得
    这两天心血来潮想要装个WSA(安卓windows子系统),原来一直用的安卓模拟器(mumu啊蓝叠啊逍遥啊),但感觉像wsa这种安卓系统与主系统融合的模式更带感,于是开始了我艰苦的安装(瞎捯饬)之......
  • 【转】PageOffice实现文件在线安全预览——禁止编辑、下载、复制等
    实际应用中,很多时候不仅需要在线编辑文件,还需要在线安全预览文件,要求不能复制、下载、另存到本地等情况。pageoffice可以实现文件在线安全预览,禁止:编辑、复制、粘贴、右键......
  • antdvue对a-form自定义组件进行校验
    在form表单中引入自定义组件时,并打算对其值进行校验,可是使用rules字段时,发现大多都校验不了表单默认触发change事件,我们需要在自定义组件中触发form提供的onFieldChange事......
  • 《跟老卫学 HarmonyOS 开发》:DevEco Studio 启用Java预览器
    老版的DevEcoStudio只支持layout资源类型的XML文件的预览。在新版的DevEcoStudio已经能够支持 Ability/AbilitySlice的Java类文件的预览。新版的DevEcoStudio默认......
  • HarmonyOS初探07——使用DevEco Studio预览器
    在前面的教程中,我们已经认识到了如何来创建一个最为简单的HelloWorld应用,并且通过Car模拟器成功运行了应用。效果如下:        但是使用模拟器运行了应用有一个缺点,......
  • 在线HTML代码预览
    @目录对于非前端开发对于非前端开发如果你不是前端开发,比如是安卓程序员,你又不是很精通HTMTL,想做一个功能的时候,总有一些时候不知道HTML页面长的样子,只有标签元素,想预览......
  • Android studio装了Markdown Editor插件还是不能预览的解决办法
    双击shift按钮,在弹窗中选中Actions,然后在输入框中输入ChooseBootJavaRuntimefortheIDE,回车后就会弹出一个新的弹窗。在这个弹窗中点击下拉列表,选中一个和你的Androi......