zoukankan      html  css  js  c++  java
  • 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 {
         auto;
    }
    </style>
    
  • 相关阅读:
    学习ActiveMQ(七):JMS消息的事务管理
    2020杭电多校第三场题解
    2020牛客暑期多校训练营第六场题解
    2020杭电多校第二场题解
    2020 Petrozavodsk Winter Camp, Jagiellonian U Contest 部分题解
    Educational Codeforces Round 86 (Rated for Div. 2) 部分题解
    2020, XIII Samara Regional Intercollegiate Programming Contest 题解
    2019 JUST Programming Contest题解
    2019-2020 ICPC Asia Taipei-Hsinchu Regional Contest 部分题解
    2019 ICPC Universidad Nacional de Colombia Programming Contest 题解
  • 原文地址:https://www.cnblogs.com/lbx6935/p/15632492.html
Copyright © 2011-2022 走看看