Vue批量上传文件及实时进度

<template>
  <el-row>
    <el-col :span="12">
      <el-upload class="upload-demo" ref="upload" :action="upload_url" drag multiple :auto-upload="false" :on-change="beforeUpload"
        :file-list="updatefile" :http-request='uploadFile'>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text" style="margin-bottom:15px">将文件拖到此处,或<em>点击上传</em></div>
        <el-progress :percentage="percentage" v-if="upload"></el-progress>
        <div class="el-upload__tip" slot="tip">
          批量文件上传,只能上传固件
          <el-button style="margin-left: 10px" size="small" type="success" @click="submitUpload">点击上传</el-button>
        </div>
      </el-upload>
    </el-col>
  </el-row>
</template>

<script>
export default {
    data() {
        return {
            fileData: '',
            upload_url: `${process.env.VUE_APP_DEVICE_BACKEND}/sys/uploads`,
            updatefile: [],
            loading: null,
            upload: false,
            percentage: 0
        };
    },
    created() {},
    methods: {
        uploadFile(file) {
            this.fileData.append('file', file.file);
        },
        submitUpload() {
            this.$confirm('确认要上传吗?', '提示', {
                type: 'info'
            })
                .then(() => {
                    var files = this.$refs.upload.uploadFiles;
                    if (files.length == 0) {
                        this.$message.error('请选择文件!');
                        return;
                    }

                    this.loading = this.$loading({
                        lock: true,
                        text: '上传中...',
                        spinner: 'el-icon-loading',
                        background: 'rgba(0, 0, 0, 0.7)'
                    });

                    this.fileData = new FormData();
                    this.$refs.upload.submit();

                    this.upload = true;
                    this.$axios
                        .post(this.upload_url, this.fileData, {
                            onUploadProgress: (data) => {
                                this.percentage = ((data.loaded / data.total) * 100) | 0;
                            }
                        })
                        .then((res) => {
                            if (res.code === 0) {
                                var html = '<div>';
                                res.data.map((item) => {
                                    if (item.is_ipload) {
                                        html += `<p style='color: green;'>文件:${item.file_name},状态:上传成功;</p>`;
                                    } else {
                                        html += `<p style='color: #ff6868;'>文件:${item.file_name},状态:上传失败,错误信息:${item.error_message};</p>`;
                                    }
                                });
                                html += '</div>';
                                this.$alert(html, '上传结果', {
                                    dangerouslyUseHTMLString: true,
                                    customClass: 'msgbox'
                                });

                                this.fileData = '';
                                this.updatefile = [];
                            } else {
                                this.$alert(res.meta.error || '上传失败', '提示');
                            }
                            this.loading.close();
                            this.upload = false;
                        })
                        .catch((err) => {
                            this.$message.error('上传失败,网络请求错误');
                            this.loading.close();
                            this.upload = false;
                        });
                })
                .catch(() => {});
        },
        beforeUpload(file) {
            let arr = file.name.split('.');
            let bin = arr[arr.length - 1];

            if (bin != 'bin') {
                this.$message.error('上传文件只能是固件!');
                this.updatefile = [];
                return false;
            }
            return bin;
        }
    }
};
</script>
<style>
.msgbox {
    width: auto;
}
</style>
posted @ 2021-12-02 11:33  进阶的哈姆雷特  阅读(1127)  评论(0编辑  收藏  举报