Element-ui使用上传时弹框选择文件类型

实现效果

1,点击上传,上传文件;
在这里插入图片描述
2,选择文件;
在这里插入图片描述
3,弹框选择文件类型;
在这里插入图片描述
在这里插入图片描述
4,选择类型后确定上传;
在这里插入图片描述

一,上传

跳过;

二,定义弹框+下拉框

1,定义属性

dialogVisible: false, //初始页面关闭弹框

在这里插入图片描述
2,定义弹框

el-autocomplete 组件在 input输入框中,’带输入建议‘;

dialogVisible:属性; @close=“decisionTyoe(false)”:点击×时调用关闭函数传入false,停止上传;
@click=“decisionTyoe(false)”:点击取消时调用关闭函数传入false,停止上传;
@click=“decisionTyoe(true)”:点击取消时调用关闭函数传入false,上传文件;

    <el-dialog title="选择类型" :visible.sync="dialogVisible" @close="decisionTyoe(false)">
      <el-autocomplete class="feed-word-type" v-model="m_arrWordTypeValue"
                       :fetch-suggestions="doWordTypeValue"
                       @select="handleQueryWordType">
      <template slot="prepend">文件类型</template>
    </el-autocomplete>

      <span slot="footer" class="dialog-footer">
        <el-button @click="decisionTyoe(false)">取 消</el-button>
        <el-button type="primary" @click="decisionTyoe(true)">确 定</el-button>
      </span>
    </el-dialog>

3,函数定义
注意!!!一定要开启监听,原因是后续的方法需要根据监听来判断是否选择了文件类型(取消,确定)

    decisionTyoe(flag){
      this.dialogVisible= false;
      // 开启监听
      this.$emit('dialog-closed', flag);
    }

4,下拉框框函数定义

	//下拉框数据
    doWordTypeValue(queryString, cb) {
      var results = [{
        value: 'IOT',
        file_type: 1,
      }, {
        value: 'MCU',
        file_type: 2,
      }, {
        value: '时序',
        file_type: 3,
      }]
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    //选择下拉框
    handleQueryWordType(inItem){
      this.m_arrWordTypeValue = inItem.value;
      this.extraData.file_type = inItem.file_type;
    },

三,上传合并弹框

1,弹框选择要在上传后台前,所以要使用到 上传组件的before-upload属性(上传文件之前的钩子,参数为上传的文件,若返回 false 或者返回 Promise 且被 reject,则停止上传。)

              <el-upload
                :action="m_uploadUrl"
                :auto-upload="true"
                :data="extraData"
                :show-file-list="false"
                :before-upload="doImportBefore"
                :on-success="doImportSuccess">
                <i class="el-icon-upload2">上传</i>
              </el-upload>

2,定义doImportBefore函数来选择上传文件与打开弹框

    doImportBefore(inFile) {
      // 打开对话框
      this.dialogVisible = true;

      // 需要来进行等待用户弹框选择文件类型;
      // 使用Promise函数等待监听dialog-closed
      return new Promise((resolve, reject) => {
      	// 当用户点击了确定或取消时触发监听,
        this.$once('dialog-closed', (confirmed) => {
          if (confirmed) {
            console.log('用户点击了确认按钮');

            ///
			// 文件的逻辑处理
            let nPos = inFile.name.lastIndexOf('.');
            if (nPos < 0) {
              this.$message.error('支持的文件格式 => dat 或 bin');
              resolve(false);
              return;
            }
            let strExt = inFile.name.substring(nPos + 1);
            strExt = strExt.toLowerCase();

            if (strExt !== 'dat' && strExt !== 'bin') {
              this.$message.error('支持的文件格式 => dat 或 bin');
              resolve(false);
              return;
            }
            ///

            // 直接设置异步加载状态...
            this.m_bIsLoading = true
            resolve(true);
            return;
          } else {
            this.$message.error('用户取消了上传');
            reject(false);
            return;
          }
        });
      });

    }
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例,演示如何使用Element UI的表单和对话框制作二级弹窗: 1. 首先,你需要在Vue项目中安装Element UI,你可以使用npm命令进行安装: ``` npm install element-ui --save ``` 2. 在你的Vue项目中引入Element UI: ```javascript import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 3. 在模板中添加一个按钮,点击该按钮会弹出对话框: ```html <template> <div> <el-button @click="showDialog">打开对话框</el-button> </div> </template> ``` 4. 在script标签中添加相关代码: ```javascript <script> export default { data() { return { dialogVisible: false, // 控制对话框的显示与隐藏 form: { // 表单数据 name: '', age: '' } } }, methods: { showDialog() { this.dialogVisible = true // 显示对话框 }, handleSubmit() { // 处理表单提交 console.log(this.form) this.dialogVisible = false // 隐藏对话框 }, handleCancel() { // 取消按钮的回调函数 this.dialogVisible = false // 隐藏对话框 } } } </script> ``` 5. 在模板中添加对话框组件: ```html <template> <div> <el-button @click="showDialog">打开对话框</el-button> <el-dialog title="表单" :visible.sync="dialogVisible"> <el-form :model="form" ref="form" label-width="80px"> <el-form-item label="姓名"> <el-input v-model="form.name"></el-input> </el-form-item> <el-form-item label="年龄"> <el-input v-model="form.age"></el-input> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click="handleCancel">取 消</el-button> <el-button type="primary" @click="handleSubmit">确 定</el-button> </div> </el-dialog> </div> </template> ``` 这样就完成了一个简单的二级弹窗,其中包含一个表单和一个对话框。当按钮被点击,对话框会弹出,用户填写表单后,点击“确定”按钮提交表单数据,同对话框被隐藏。当用户点击“取消”按钮,对话框也会被隐藏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值