element ui (简单的手动上传文件,submit方式)

<template>
    <el-form ref="form" label-position="left" :model="form" :rules="rules" label-width="80px"  style="margin:20px;width:60%;min-width:600px;">
        <el-form-item label="坐标类型" prop="coordinateType">
            <el-radio-group v-model="form.coordinateType">
                <el-radio label="BD09">百度</el-radio>
                <el-radio label="GCJ02">谷歌</el-radio>
                <el-radio label="GW84">GW84</el-radio>
                <el-radio label="Mercat">墨卡托</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="上传文件" prop="excelFile">
            <el-upload 
                class="upload-demo"
                ref="upload"
                :action="uploadUrl()"
                name="excelFile"
                drag
                :data="upData"
                :file-list="fileList"
                :on-error="uploadFalse"
                :on-success="uploadSuccess"
                :auto-upload="false"
                :before-upload="beforeAvatarUpload">
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
                <!-- <el-button slot="trigger" size="small" >选取文件</el-button> -->
                <div slot="tip" class="el-upload__tip" style="color:red">上传文件只能是 xls、xlsx、txt 格式!</div>
            </el-upload>
        </el-form-item>
        
        <el-form-item>
            <el-button type="primary" @click="submitUpload(form)">导入</el-button>
            <el-button   @click="onCancel(form)">取消</el-button>
        </el-form-item>
    </el-form>
</template>
 

<script>
    export default {
        data() {
            return {
                rules: {
                    coordinateType: [
                        { required: true, message: '请选择文件中的坐标类型', trigger: 'change' }
                    ],
                },
                form: {
                    fileName:'',
                    coordinateType: 'BD09',
                },
                fileList: [],
            }
        },
        computed: {
            // 这里定义上传文件时携带的参数,即表单数据
            upData: function() {
                return this.form
            }
        },
        methods: { 
            //导入接口地址
            uploadUrl: function() {
                return ‘http://172.16.82.63:8080/uploadPath‘  //接口
            },
            //文件上传成功触发
            uploadSuccess(response, file, fileList) {
                console.log(response)
                if(response.code == 0){
                    this.$message({
                        message: '导入成功',
                        type: 'success'
                    });
                }else {
                     this.$message({
                        message: '导入失败',
                        type: 'error'
                    });
                }
            },
             //文件上传失败触发
            uploadFalse(response, file, fileList) {
                 this.$message({
                        message: '文件上传失败!',
                        type: 'error'
                    });
            },
            // 上传前对文件的大小和类型的判断
            beforeAvatarUpload(file) {
                this.form.fileName = file.name;
                const extension = file.name.split(".")[1] === "xls";
                const extension2 = file.name.split(".")[1] === "xlsx";
                const extension3 = file.name.split(".")[1] === "txt";
                if (!extension && !extension2 && !extension3) {
                this.$message({
                        message: '上传文件只能是 xls、xlsx、txt 格式!',
                        type: 'error'
                    });
                }
                return extension || extension2 || extension3 ;
            },
            //表单提交
            submitUpload(form) {
               this.$refs.form.validate((valid) => {
                    if (valid) {
                        //触发组件的action
                        this.$refs.upload.submit();  //主要是这里
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            //表单取消
            onCancel(form){
                this.$refs.form.resetFields();
            }



        }
    }
</script>

 

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读