element导入实现方法

本文介绍了一种基于Vue框架的文件导入组件实现方案,详细解释了如何使用Element UI中的按钮和对话框组件来创建文件导入功能。具体包括:点击导入按钮触发文件选择对话框、文件列表展示、文件预览与移除操作以及文件上传到服务器的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击导入按钮出现导入弹出框 选择好文件之后点击确定上传服务器同时窗口关闭

<el-button size="mini" @click="toLead = true">导入</el-button>
 <el-dialog
            title="导入"
            :visible.sync="toLead"
            width="30%"
            center>
            <el-upload
            class="upload-demo"
            ref="upload"
            action=" "
            :on-change = 'changeFile'
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            :auto-upload="false">
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
            </el-upload>
            <span slot="footer" class="dialog-footer">
                <el-button @click="toLead = false">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
            </span>
            </el-dialog>
export default {
        name: "文件名",
        data(){
            return {
                toLead:false,
            }  
        },
        methods: {
            //上传
            changeFile(file){
                this.fileList.push(file)
            },
            submit(){
                let file = this.fileList[0];
                let param = new FormData();
                param.append("file", file.raw);
                console.log(param.get("file"));
                let config = {
                //添加请求头
                headers: { "Content-Type": "multipart/form-data" },
                };
                //我这边后台需要先把上传的文件名发送给后台然后返回给我url地址
                    this.$axios.post('地址'+ file.name,param).then(result => {
                        this.toLead = false;
                        var $result = result.data.data
                        console.log($result.url)
                        this.$axios({
                        //然后我通过返回给我的url地址实现上传功能
                        url:'地址',
                        method: 'post',
                        data: {
                            param:JSON.stringify({
                                fileName:file.name,
                                url:$result.url,
                                companyId:'7'
                            })
                        },
                        headers:{
                        	//token我这边必传参数
                            Authorization:this.$cookie.get('token'),
                        }
                        }).then(result =>{
                        	//上传成功
                            console.log(result)
                            alert(result.data.data.msg)
                            
                        }).catch((error)=>{
                            alert('请求失败')
                            //判断是否是401
                        });

                    }).catch(error => {
                    alert('请求失败!');
                });
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
                this.fileList.splice(0,1)
            },
            handlePreview(file) {
                console.log(file);
            },
        },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值