VUE 上传多个附件,显示文件列表,可删除

时间:2024-10-22 08:57:24
<template>
    <div class="row">           
        <form action="/" method="post"  enctype="multipart/form-data">
            <div class="file-box">上传附件
                <input type="file"   class="file-btn" name="files" accept="image/x-png,image/gif,image/jpeg,image/bmp" placeholder="file" multiple @change="tirggerFile($event)">
            </div>
            <input type="button"  value="fileUpload" @="uploadFil">
        </form>
        <ul class="upload-list">
            <li  v-for="(file, index) in fileList" class="upload-list-li">
                <a class="upload-list-li-name"><i class="fa fa-file-text-o" aria-hidden="true"></i>{{}}</a>
                <label class="upload-list-li-label" @click="deleFile(index,)"><i class="fa fa-close"></i></label>
            </li>
        </ul>
    </div>    
</template>
<script type="text/javascript">
    export default {
        name:'upload-file',
        data() {
            return {
               fileList:[]
            }
        },
        methods:{
            tirggerFile(event){
                var _this=this;
                var list=;             
                for(var i=0;i<;i++){
                    if(_this.>=5){
                        _this.('最多上传5个文件!');
                        return false;
                    }else{
                        _this.(list[i]);
                    }
                }
            },
            deleFile(i,name){
                (i,1);
            },           
            uploadFil(){
                var _this=this;
                var formData=new formData();
                 for(var i=0;i<;i++){
                     _this.('file',[i]);
                 }
                $.ajax({
                    url: 'http://localhost:8080/Pictures',
                    type: 'POST',
                    data: formData,
                    async: false,
                    cache: false,
                    contentType: false,
                    processData: false,
                    beforeSend: function(xhr) {
                        ("tok",token);
                    },
                    success: function (data) {
                        ((data));
                    },
                    error: function (data) {
                        ((data));
                    }
                });
            },
        }
    }
</script>
<style scoped>
.upload-list{margin:0;padding:0;list-style:none;}
.upload-list-li{position: relative;margin-top:5px;line-height: 1.8}
.upload-list-li:hover{background-color: #f5f7fa;}
.upload-list-li-name{position:relative;margin-right: 40px;display: block;}
.fa-file-text-o{margin-right:7px;}
.upload-list-li-label{position:absolute;right:5px;top:0;}
.file-box{
    padding:5px 15px;
    border-radius:3px;
    background-color:#409eff;
    border-color:#409eff;
    color:#000;display:inline-block;position: relative;
}
.file-btn{
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    outline: none;
    background-color: transparent;
    filter:alpha(opacity=0);
    -moz-opacity:0;
    -khtml-opacity: 0;
    opacity: 0;
}
</style>