ant-design-vue 之upload 文件上传


ant-design-vue 之upload 文件上传

01) 单文件上传

使用  :before-upload="beforeUpload"  和  @change="handleChange"

<template>
    <div>
        
        <div> 图片名字: {{imgName}}</div> <br /><br /><br />
        
        <a-upload
                name="file"
                :multiple="true"
                :before-upload="beforeUpload"
                :file-list="fileList"
                @change="handleChange">
            <a-button> <a-icon type="upload" /> Click to Upload </a-button>
        </a-upload>
    </div>
</template>
<script>
    
    /* 这是ant-design-vue */
    import Vue from 'vue'
    import Antd, { message,Select } from 'ant-design-vue'  //这是ant-design-vue
    import 'ant-design-vue/dist/antd.css'
    Vue.use(Antd);
    /* 这是ant-design-vue */

    
    export default {
        components:{},
        data() {
            return {
                imgName: "",
                fileStatus: true,
                fileList: [],
            }
        },
        methods: {
            
            beforeUpload(file) {
                if (file.type !== 'image/jpeg') {
                    this.fileStatus = false;
                    this.$message.error('只能上传 JPG 格式');
                }
                this.imgName = file.name;
                // 禁用原来上传,改用自定义上传,[ 必须返回false,才能在handleChange使用自己的请求方式]
                return false; 
            },
            handleChange(info) {
                if (this.fileStatus) {
                    // 开始上传
                    let fileList = [...info.fileList];
                    const formData = new FormData();
                    formData.append("file", fileList[0].originFileObj);

                    // this.$post("上传URL地址", formData).then(resUpLoadFiles => {
                    //     console.log(resUpLoadFiles);
                    // })
                    fileList = fileList.map(file => {
                        file.url = "https://www.cnblogs.com/images/jblogo.png";
                        return file;
                    });
                    this.fileList = fileList;
                }
            },
        },
    };
</script>

<style scoped>
 
</style>
View Code

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM