• vue-使用input封装上传文件图片全局组件


    前言

    • 实际开发过程中,我们经常遇见需要上传文件图片功能,可以封装一个全局组件来调用

    • 原理很简单,首先获取到文件或图片对象,调用自己公司文档服务器的接口,上传文件图片

    • 为了方便用户体验,我们应该在上传之前开启遮罩层,上传成功之后关闭遮罩层。

    • 我们还可以根据实际开发场景自定义把url和文件名称传回父组件

    代码实现

    1.定义api
    1. export function uploadvideo (data) {
    2. return request({
    3.   url: '/upload/video',
    4.   method: 'post',
    5.   headers: { 'Content-Type': 'multipart/form-data' },
    6.   data
    7. })
    8. }
    2.在src/components/建立DocUpload文件夹/index.vue-代码如下
    1. <template>
    2. <el-dialog
    3.   title="上传"
    4.   :visible.sync="dialogVisible"
    5.   width="40%"
    6.   :before-close="handleClose"
    7. >
    8.   <el-form ref="form" :model="form" size="small" label-width="80px">
    9.     <el-form-item label="文件名称:">
    10.       <el-input v-model="form.contitle" disabled>el-input>
    11.     el-form-item>
    12.     <el-form-item label="文件上传:">
    13.       <div class="uppicture">
    14.         <input type="file" class="upinput" ref="file" @change="showimg" />
    15.         <i class="el-icon-plus" id="changes" @click="changeimg">i>
    16.         <p>上传合同文件附件p>
    17.       div>
    18.       <el-button type="primary" class="uploadbutton" @click="addupload"
    19.         >上传附件
    20.       >
    21.     el-form-item>
    22.   el-form>
    23.   <span slot="footer" class="dialog-footer">
    24.     <el-button @click="handleClose" style="background: #f7f7f7" size="small"
    25.       >取 消
    26.     >
    27.    
    28.   span>
    29. el-dialog>
    30. template>
    31. <script>
    32. import { uploadvideo } from '@/api/entering'
    33. // 遮罩层
    34. import { Loading } from 'element-ui'
    35. export default {
    36. name: 'DocUpload',
    37. data () {
    38.   return {
    39.     form: {
    40.       // 合同名称
    41.       contitle: ''
    42.     },
    43.     formdata: {}
    44.   }
    45. },
    46. props: {
    47.   // 显示隐藏
    48.   dialogVisible: {
    49.     type: Boolean,
    50.     //   必传
    51.     required: true
    52.   }
    53. },
    54. methods: {
    55.   // 关闭之前
    56.   handleClose () {
    57.     console.log('关闭之前')
    58.     // .sync语法糖,单向数据流问题,
    59.     // 父组件传递给子组件的数据,子组件直接修改会报错,需要传递给父组件修改
    60.     this.$emit('update:dialogVisible', false)
    61.   },
    62.   // 输入款获取事件
    63.   showimg () {
    64.     const that = this
    65.     console.log(that.$refs.file)
    66.     console.log(that.$refs.file.files[0])
    67.     // 文件名称复制
    68.     that.form.contitle = that.$refs.file.files[0].name
    69.     // 声明一个formdata对象
    70.     this.formdata = new FormData()
    71.     // 赋值需要传递的文件
    72.     this.formdata.append('multipartFile', that.$refs.file.files[0])
    73.   },
    74.   // 图标触发输入框事件
    75.   changeimg () {
    76.     // 点击图标时候,触发input选择文件按钮
    77.     this.$refs.file.dispatchEvent(new MouseEvent('click'))
    78.   },
    79.   // 上传附件
    80.   async addupload () {
    81.     // 上传文文件提示,未选择文件提示用户
    82.     if (!this.form.contitle) {
    83.       return this.$message.warning('请先在左侧上传文件')
    84.     }
    85.     //开启遮罩层
    86.     let loadingInstance = Loading.service({
    87.       lock: true, //lock的修改符--默认是false
    88.       text: '正在上传文件,请耐心等待', //显示在加载图标下方的加载文案
    89.       spinner: 'el-icon-loading', //自定义加载图标类名
    90.       background: 'rgba(0, 0, 0, 0.7)', //遮罩层颜色
    91.       target: document.querySelector('#futureTransferDialog') //loadin覆盖的dom元素节点
    92.     })
    93.     const res = await uploadvideo(this.formdata)
    94.     console.log('文档服务器上传文件', res)
    95.     // 传递文件存储id
    96.     this.$emit('updataurl', res.data.url)
    97.     // 回显文件名称给父组件的form表单
    98.     this.$emit('updata', this.form.contitle)
    99.     // 清空表单
    100.     this.form.contitle = ''
    101.     this.formdata = {}
    102.     // 关闭弹框
    103.     this.handleClose()
    104.     //关闭遮罩层
    105.     loadingInstance.close()
    106.   }
    107. }
    108. }
    109. script>
    110. <style lang="scss" scoped>
    111. ::v-deep .el-dialog {
    112. border-radius: 10px;
    113. .el-dialog__header {
    114.   border-radius: 9px 9px 0 0;
    115.   background-color: #1488c6;
    116.   padding: 8px 20px;
    117.   .el-dialog__title {
    118.     color: white;
    119.     font-size: 16px;
    120.   }
    121.   .el-dialog__headerbtn {
    122.     top: 12px;
    123.     i {
    124.       color: white;
    125.     }
    126.   }
    127. }
    128. .el-dialog__footer {
    129.   text-align: center;
    130. }
    131. .el-dialog__body {
    132.   padding: 12px;
    133. }
    134. .uppicture {
    135.   width: 120px;
    136.   height: 120px;
    137.   border: 1px solid #717376;
    138.   position: relative;
    139.   cursor: pointer;
    140.   input {
    141.     width: 100%;
    142.     height: 100%;
    143.     vertical-align: middle;
    144.     opacity: 0;
    145.   }
    146.   i {
    147.     position: absolute;
    148.     top: 50%;
    149.     left: 50%;
    150.     transform: translate(-50%, -50%);
    151.     font-size: 30px;
    152.     // background-color: skyblue;
    153.   }
    154.   p {
    155.     position: absolute;
    156.     bottom: -2px;
    157.     left: 50%;
    158.     word-break: keep-all;
    159.     transform: translate(-50%);
    160.   }
    161.   .uploadbutton {
    162.     position: absolute;
    163.     bottom: 0;
    164.     margin-left: 20px;
    165.     position: relative;
    166.   }
    167.   &:hover {
    168.     color: #2da9fa;
    169.     border: 1px solid #2da9fa;
    170.     p {
    171.       color: #2da9fa;
    172.     }
    173.   }
    174. }
    175. .uploadbutton {
    176.   position: absolute;
    177.   top: 70%;
    178.   left: 150px;
    179. }
    180. }
    181. style>
    3.全局组件注册-省略
    4.父组件使用
    4.1组件使用
    1. <DocUpload
    2.     :dialogVisible.sync="dialogannex"
    3.     // form是父组件表单上传成功之后直接通过子传父把url和文件名称传递到父组件表单
    4.     @updata="form.name = $event"
    5.     @updataurl="form.ontractAttachment = $event"
    6.   >DocUpload>
    4.2父组件data
    1. // 上传组件开关
    2. dialogannex: false,
    3. // 表单
    4. form: {},
    4.3打开组件-methods
    1. addupload () {
    2.     this.dialogannex = true
    3. },

    总结:

    经过这一趟流程下来相信你也对 vue-使用input封装上传文件图片全局组件 有了初步的深刻印象,但在实际开发中我 们遇到的情况肯定是不一样的,所以我们要理解它的原理,万变不离其宗。加油,打工人!

    有什么不足的地方请大家指出谢谢 -- 風过无痕

  • 相关阅读:
    Go用两个协程交替打印100以内的奇偶数
    程序员的孤独你不懂
    微信小程序提示确认框 wx.showModal
    如何在数据库中存储小数:FLOAT、DECIMAL还是BIGINT?
    远程拷贝Windows上的文件到Linux指定的文件夹
    yolov3学习笔记
    MySQL:远程连接数据库(2)
    用迅为RK3568开发板使用OpenCV处理图像颜色通道提取ROI
    数据排序 归并排序,计数排序以及快速排序的三路优化
    图像拼接Image switch(opencv配置,有关的函数只在低版本中存在,例如3.4.2.16)
  • 原文地址:https://blog.csdn.net/weixin_53579656/article/details/134563155