Vue el-upload 上传文件

自己写的一个小demo,拷贝到html直接打开即可测试运行。

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入最新版本样式 -->


    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <title>Document</title>
    <style>
        .content {
            width: 300px;
            height: 50px;
            font-size: 20px;
            display: flex;
            line-height: 50px;
        }

        .contentName {
            width: 50px;
        }
    </style>
</head>

<body>
    <div id="app">
        <el-col :span="4" :xs="24">
            <el-input v-model="uploadData.msg" type="text" placeholder="所传信息" />
        </el-col>

        <el-upload class="upload-demo" ref="upload" action="URL" :on-preview="handlePreview" :on-remove="handleRemove"
            :on-change="handleChange" :file-list="fileList" :data="uploadData" :auto-upload="false"
            :on-success="success" :multiple="false" :on-error="error">
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
            <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
        </el-upload>

    </div>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入最新版本组件库 -->
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>

    <!-- 固定版本样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui@2.12.0/lib/theme-chalk/index.css">
    <!-- 固定版本组件库 -->
    <script src="https://unpkg.com/element-ui@2.12.0//lib/index.js"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                fileList: [],
                // 上传时附带的额外参数
                uploadData: {
                    type: 1,
                    msg: "",

                }

            },
            methods: {
                submitUpload() {
                    // 请求加的参数this.uploadData
                    console.log(this.uploadData)
                    // 看看这个时候的请求地址是否正确上面的action
                    this.$refs.upload.submit();
                },
                handleRemove(file, fileList) {
                    console.log(file, fileList);
                },
                handlePreview(file) {
                    console.log(file);
                },
                //上传成功时弹框
                success(res, file, fileList) {
                    // 在文件上传成功后,可以从response的data属性获取图片在服务器上的路径,文件的具体信息在file.raw中
                    console.log(res, file, fileList)
                },
                error(err, file, fileList) {
                    console.log(file, "失败信息")
                },
                //上传文件让第二次覆盖第一的文件
                handleChange(file, fileList) {
                    if (fileList.length > 0) {
                        this.fileList = [fileList[fileList.length - 1]]
                    }
                }
            },

        })
    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值