最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue+el-upload實現(xiàn)多文件動態(tài)上傳

 更新時間:2021年10月18日 09:10:51   作者:ichuany  
這篇文章主要為大家詳細介紹了vue+el-upload實現(xiàn)多文件動態(tài)上傳,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

vue+el-upload多文件動態(tài)上傳,供大家參考,具體內(nèi)容如下

使用場景

點擊【添加/刪除】,可動態(tài)增加/刪除行數(shù),并為每行上傳附件,附件暫存前端,點擊【上傳】可以將所有附件和部分名稱同時提交后臺,實現(xiàn)表格的動態(tài)多文件上傳。其中el-upload ,相關(guān)鉤子函數(shù)可查看el-upload 官方文檔

這里的表格行的新增是在彈框中填完再寫入的,也可直接在表格中添加行,然后填寫內(nèi)容(template slot-scope=“scope” 注釋部分代碼),這里僅提供思路

代碼html部分

<div class="vue-box">
           <div class="title-line">
                            其他必須持有的證照<el-button type="primary" size="mini" style="height: 20px;padding-top: 3px !important;margin-left: 50px" icon="el-icon-plus" @click="handleAddDetails">添加行</el-button>
                            <el-button type="primary" size="mini" style="height: 20px;padding-top: 3px !important;margin-left: 50px" icon="el-icon-plus" @click="doFileUpload()">上傳</el-button>
            </div>
            <el-table
                    :row-class-name="rowClassNameDeal"
                    :data="tableData"
                    style="width: 100%;">
                <el-table-column
                        prop="id"
                        width="50"
                        label="序號">
                </el-table-column>
                <el-table-column
                        prop="cardName"
                        width="180"
                        label="證照名稱">
                    <!--<template slot-scope="scope">
                        <el-input size="mini" v-model="tableData[scope.row.id-1].cardName"></el-input>
                    </template>-->
                </el-table-column>
                <el-table-column
                        prop="cardNo"
                        width="180"
                        label="證件號碼">
                    <!--<template slot-scope="scope">
                        <el-input size="mini" v-model="tableData[scope.row.id-1].cardNo"></el-input>
                    </template>-->
                </el-table-column>
                <el-table-column
                        prop="startDate"
                        width="180"
                        label="起始日期">
                    <!--<template slot-scope="scope">
                        <el-date-picker
                                v-model="tableData[scope.row.id-1].startDate"
                                style="width: 80%;"
                                size="mini"
                                value-format="yyyy-MM-dd"
                                type="date"
                                placeholder="選擇日期">
                        </el-date-picker>
                    </template>-->
                </el-table-column>
                <el-table-column
                        prop="endDate"
                        width="180"
                        label="結(jié)束日期">
                    <!--<template slot-scope="scope">
                        <el-date-picker
                                v-model="tableData[scope.row.id-1].endDate"
                                style="width: 80%;"
                                size="mini"
                                value-format="yyyy-MM-dd"
                                type="date"
                                placeholder="選擇日期">
                        </el-date-picker>
                    </template>-->
                </el-table-column>
                <el-table-column
                        prop="address"
                        label="附件">
                    <template slot-scope="scope">
                        <el-upload
                                :ref="scope.row.cardName"
                                :http-request="dynamicUpload"
                                :before-upload="beforeUploadFile(scope.row)"
                                :on-remove="uploadRemove"
                                :before-remove="uploadBeforeRemove"
                                :on-preview="uploadPreview"
                                name="upload"
                                :limit="1"
                                :data="scope.row.cardName"
                                :on-exceed="uploadHandleExceed"
                                :file-list="tableData[scope.row.id-1].fileUploadedList">
                            <el-button  size="mini" type="info">點擊上傳</el-button>
                        </el-upload>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="date"
                        width="80"
                        label="操作">
                    <template slot-scope="scope">
                        <el-button size="mini" type="warning" @click="handleDeleteDetails(scope.row)">刪除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            
            <el-dialog title="證照信息" :visible.sync="addCardVisible" width="40%">
                <el-form :model="fileInfo">
                    <el-form-item label="證照名稱" :label-width="labelWidth">
                        <el-input v-model="fileInfo.cardName" autocomplete="off" style="width: 100%;"></el-input>
                    </el-form-item>
                    <el-form-item label="證件號碼" :label-width="labelWidth">
                        <el-input v-model="fileInfo.cardNo" autocomplete="off" style="width: 100%;"></el-input>
                    </el-form-item>
                    <el-form-item label="生效日期" :label-width="labelWidth">
                        <el-date-picker type="date" placeholder="生效日期" value-format="yyyy-MM-dd" v-model="fileInfo.startDate" style="width: 100%;"></el-date-picker>
                    </el-form-item>
                    <el-form-item label="失效日期" :label-width="labelWidth">
                        <el-date-picker type="date" placeholder="失效日期" value-format="yyyy-MM-dd" v-model="fileInfo.endDate" style="width: 100%;"></el-date-picker>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="addCardVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addFileDetail">確 定</el-button>
                </div>
            </el-dialog>
</div>

js部分代碼

let nodeVue = new Vue({
    el: '.vue-box',
    data: {
        labelWidth: '150px',
        tableData: [],
        uploadParams:{
            fileTagName: ''
        },
        totalFileList:[],
        totalFileNameList:[],
        addCardVisible:false,
        fileInfo:{
            cardName:'',
            cardNo:'',
            startDate:'',
            endDate:''
        }
    },
    methods:{
        // 文件相關(guān)
        uploadRemove:function(file) {
            let that = this;
            // 刪除文件列表中的文件
            for(let i=0;i<that.totalFileNameList.length;i++){
                if(that.totalFileNameList[i].fileName === file.name){
                    that.totalFileNameList.splice(i,1)
                }
            }
            for(let i=0;i<that.totalFileList.length;i++){
                if(that.totalFileList[i].name === file.name){
                    that.totalFileList.splice(i,1)
                }
            }
        },
        // 上傳文件參數(shù)設置
        beforeUploadFile:function(row) {
            console.log(row.cardName);
            this.uploadParams.fileTagName=row.cardName
            this.uploadParams.fid=row.id
        },
        // 下載文件,點擊文件列表中的文件下載
        uploadPreview:function(file){
            console.log(file);
        },
        uploadHandleExceed:function(files, fileList) {
            this.$message.warning(`當前限制選擇 1 個文件`);
        },
        uploadBeforeRemove:function(file) {
            return this.$confirm(`確定移除 ${ file.name }?`);
        },
        // 附件添加行,打開添加行彈框
        handleAddDetails(){
            let that = this;
            that.addCardVisible = true;
            that.fileInfo.cardName = '';
            that.fileInfo.cardNo = '';
            that.fileInfo.startDate = '';
            that.fileInfo.endDate = '';
        },
        // 向表格數(shù)據(jù)中添加一行
        addFileDetail(){
            let that = this;
            if (that.tableData == undefined) {
                that.tableData = new Array();
            }
            let obj = {};
            obj.id = 0;
            obj.cardName = that.fileInfo.cardName;
            obj.cardNo = that.fileInfo.cardNo;
            obj.startDate = that.fileInfo.startDate;
            obj.endDate = that.fileInfo.endDate;
            obj.fileUploadedList=[];
            that.tableData.push(obj);
            that.addCardVisible = false;
        },
        // 刪除行
        handleDeleteDetails(row){
            let that = this;
            that.tableData.splice(row.id-1, 1);
            //同時 刪除文件列表及關(guān)聯(lián)列表中的數(shù)據(jù)
            let tmpFileName = '';
            for(let i=0;i<that.totalFileNameList.length;i++){
                if(that.totalFileNameList[i].cardName === row.cardName){
                    tmpFileName = that.totalFileNameList[i].fileName;// 先暫存再執(zhí)行刪除操作
                    that.totalFileNameList.splice(i,1);
                }
            }
            for(let i=0;i<that.totalFileList.length;i++){
                if(that.totalFileList[i].name === tmpFileName){
                    that.totalFileList.splice(i,1)
                }
            }
        },
        rowClassNameDeal({row, rowIndex}) { 
            //把每一行的索引放進row.id,此方法用于生成表格中的序號,當在表格中填寫內(nèi)容時,每一行都需要綁定不同的v-model
            row.id = rowIndex+1;
        },
        // 自定義上傳,只將文件暫存在前端
        dynamicUpload(content){
            let that = this;
            if(content.data.length === 0){
                that.$message.warning(`請?zhí)顚懽C照名稱?。?!`);
                that.$refs[content.data].clearFiles();
                return false;
            }
            for(let i=0;i<that.totalFileNameList.length;i++){
                if(that.totalFileNameList[i].fileName === content.file.name){
                    that.$message.warning('改文件已上傳,請重新選擇文件上傳?。?!');
                    that.$refs[content.data].clearFiles();
                    return false;
                }
            }
            // 將文件加入到待傳輸?shù)奈募斜?
            that.totalFileList.push(content.file)
            let fileNameData = {
                cardName: content.data,
                fileName: content.file.name
            }
            // totalFileNameList 存儲文件和表格內(nèi)容的關(guān)聯(lián)關(guān)系,這里只關(guān)聯(lián)了證照名稱
            that.totalFileNameList.push(fileNameData)
        },
        // 執(zhí)行上傳操作將文件和表格信息一起發(fā)送到后臺
        doFileUpload(){
            let that = this;
            if(that.totalFileList.length === 0){
                that.$message.warning("請上傳文件?。。?);
                return;
            }
            // 上傳文件需要用FormData,processData和contentType 兩個參數(shù)必須設置,否則上傳不成功
            const params = new FormData();
            // 為上傳的每個文件命名,方便后臺獲取時與表格數(shù)據(jù)關(guān)聯(lián)
            for (let i = 0; i < that.totalFileList.length; i++) {
                params.append(that.totalFileList[i].name, that.totalFileList[i]);
            }
            params.append("fileNameList", JSON.stringify(that.totalFileNameList));
            $.ajax({
                url:baseurl+"/testupload/fileUpload",
                type:"POST",
                dataType: "json",
                processData: false, //用于對data參數(shù)進行序列化處理 這里必須false
                contentType: false, //必須
                data:params,
                success:function(res){
                    that.$message.warning('上傳成功');
                }
            });
        }
    },
    created: function(){       
    }
})

后臺接收代碼

@Controller
@RequestMapping("/testupload")
public class RegisterController {

 // 附件從 request中獲取
    @RequestMapping("/fileUpload")
    @ResponseBody
    public ServerResponse fileupload(HttpServletRequest request,String fileNameList){
        try{
            if(fileNameList == null){
                throw new Exception("請選擇文件后上傳!?。?);
            }
            // 1. 上傳的位置
            String path = "E:\\uploadfile";
            //判斷該路徑是否存在
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            // 處理以字符串形式上傳的關(guān)聯(lián)數(shù)據(jù)信息
            JSONArray jsonArray = JSON.parseArray(fileNameList);
            // 遍歷關(guān)聯(lián)列表
            for(Object object : jsonArray){
                JSONObject jsonObject = JSON.parseObject(object.toString());
                System.out.println(jsonObject.getString("cardName"));
                // 獲取文件
                MultipartFile file1 = ((MultipartHttpServletRequest) request).getFile(jsonObject.getString("fileName"));
                // 獲取文件信息
                String filename = file1.getOriginalFilename();
                System.out.println(filename);
                // 保存文件
                file1.transferTo(new File(path, filename));
            }
        }catch (Exception e) {
            log.error(e.getMessage());
            return ServerResponse.createByErrorMessage(e.getMessage());
        }
        return ServerResponse.createBySuccess();
    }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • element-ui循環(huán)顯示radio控件信息的方法

    element-ui循環(huán)顯示radio控件信息的方法

    今天小編就為大家分享一篇element-ui循環(huán)顯示radio控件信息的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • vue如何修改data中數(shù)據(jù)問題

    vue如何修改data中數(shù)據(jù)問題

    這篇文章主要介紹了vue如何修改data中數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue導出excel的兩種實現(xiàn)方式代碼

    vue導出excel的兩種實現(xiàn)方式代碼

    這篇文章主要給大家介紹了關(guān)于vue導出excel的兩種實現(xiàn)方式,在項目中我們可能會碰到導出Excel文件的需求,文中給出了詳細的代碼示例,需要的朋友可以參考下
    2023-08-08
  • 如何以拖拽方式生成Vue用戶界面

    如何以拖拽方式生成Vue用戶界面

    這篇文章主要給大家介紹了關(guān)于如何以拖拽方式生成Vue用戶界面的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • Vue?3?使用moment設置顯示時間格式的問題及解決方法

    Vue?3?使用moment設置顯示時間格式的問題及解決方法

    在Vue?3中,因為過濾器(filter)已經(jīng)被廢棄,取而代之的是全局方法(global?method),本文給大家介紹Vue?3?使用moment設置顯示時間格式的問題及解決方法,感興趣的朋友一起看看吧
    2023-12-12
  • element-plus 如何設置 el-date-picker 彈出框位置

    element-plus 如何設置 el-date-picker 彈出框位置

    el-date-picker 組件會自動根據(jù)空間范圍進行選擇比較好的彈出位置,但特定情況下,它自動計算出的彈出位置并不符合我們的實際需求,故需要我們手動設置,這篇文章主要介紹了element-plus 如何設置 el-date-picker 彈出框位置,需要的朋友可以參考下
    2024-07-07
  • Vue實現(xiàn)穿梭框效果

    Vue實現(xiàn)穿梭框效果

    這篇文章主要為大家詳細介紹了Vue實現(xiàn)穿梭框效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • Vue中render函數(shù)調(diào)用時機與執(zhí)行細節(jié)源碼分析

    Vue中render函數(shù)調(diào)用時機與執(zhí)行細節(jié)源碼分析

    這篇文章主要為大家介紹了Vue中render函數(shù)調(diào)用時機與執(zhí)行細節(jié)源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • vue component 中引入less文件報錯 Module build failed

    vue component 中引入less文件報錯 Module build failed

    這篇文章主要介紹了vue component 中引入less文件報錯 Module build failed的解決方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-04-04
  • vue可視化表單設計器自定義組件使用方法

    vue可視化表單設計器自定義組件使用方法

    Vue前端開發(fā)中表單組件是排在前三的高頻使用的組件,下面這篇文章主要給大家介紹了關(guān)于vue可視化表單設計器自定義組件使用的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-12-12

最新評論

天津市| 应用必备| 友谊县| 普安县| 肃北| 噶尔县| 黎平县| 中江县| 开阳县| 兴宁市| 固镇县| 锦州市| 板桥市| 炉霍县| 进贤县| 城市| 巴青县| 邵武市| 芮城县| 喜德县| 霍邱县| 叶城县| 社旗县| 门源| 恩平市| 汽车| 江口县| 阿尔山市| 双江| 阳谷县| 囊谦县| 辽源市| 钟山县| 南投县| 信丰县| 色达县| 元朗区| 九江县| 长垣县| 靖边县| 连山|