vue+element实现多文件上传

<template>
    <div>
        <el-dialog title="111" :visible.sync="dialogVisible" width="400px" append-to-body>
            <el-upload ref="upload" :limit="20" accept=".jpg,.gif,.png,.jpeg,.txt,.pdf,.doc,.docx,.xls,.xlsx"
                name="files" :multiple="true" :action="baseURL" :headers="headers" :on-change="handleFileChange"
                :before-remove="handleFileRemove" :auto-upload="false" :file-list="upload.fileList">
                <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
            </el-upload>

            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitFileForm">确 定</el-button>
                <el-button @click="dialogVisible = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import axios from 'axios'
export default {
    data() {
        return {
            dialogVisible: false,
            upload: {
                fileList: [],
                fileName: ''
            },
            baseURL: window.env.BASE_API,
            headers: { 'Content-Type': 'multipart/form-data', token: '' }
        }
    },
    methods: {
        // 上传发生变化钩子
        handleFileChange(file, fileList) {
            this.upload.fileList = fileList;
        },
        // 删除之前钩子
        handleFileRemove(file, fileList) {
            this.upload.fileList = fileList;
        },
        // 提交上传文件
        submitFileForm() {
            // 创建新的数据对象
            let formData = new FormData();
            // 将上传的文件放到数据对象中
            // data.append("files", fileInput.files[0], "800+1200.jpg")
            this.upload.fileList.forEach(file => {
                formData.append('files', file.raw, file.name);
            });
            console.log("提交前", formData.getAll('files'))
            // 自定义上传
            this.uploadFile(formData).then(response => {
                console.log(response);
            }).catch(() => {
                this.$message.error('上传失败!');
            });

        },
        // 封装的上传请求
        uploadFile(params) {
            return axios.post(`${this.baseURL}/bookinfo/infoBase/import-data-mutl`, params,
                { headers: { 'Content-Type': 'multipart/form-data', token: '' } })
        },


    }
};
</script>

postman显示:

在这里插入图片描述

浏览器控制台显示

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值