• laravel+elementui el-upload上传文件


    注:例子中我移除了一些不必要的代码,以免过多没必要的代码影响大家的理解和阅读

    接口

    
    
    class ContractController extends Controller
    {
    	/**
         * @描述:上传文件到本地
         * @param Request $request
         */
        public function editUpload(Request $request)
        {
            try {
                $extension = $request->file('file')->extension(); //上传文件的文件扩展名
                if ($extension != 'pdf') {
                    throw new \Exception("请上传pdf格式文件");
                }
                $name = $request->file('file')->getClientOriginalName(); //上传文件的原始名称
                $path = $request->file('file')->storeAs('/uploads/renewalPlug/contract', $name, 'uploads');
                $result['cndLocation'] = '/' . $path;
                return $this->created($result);
            } catch (\Exception $e) {
                return $this->error("上传失败:" . $e->getMessage());
            }
        }
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    配置路由

    //上传合同文件
    Route::post('guild/contract/editUpload', [C\Guild\ContractController::class, 'editUpload']);
    
    • 1
    • 2

    前端调用

    <template>
        <div v-loading="loading || $store.state.error_loading" class="main">
            <el-form :model="ruleForm" status-icon ref="ruleForm" label-width="100px" class="demo-ruleForm">
            
                <el-form-item label="默认合同" prop="is_default">
                    <el-switch v-model="ruleForm.is_default" active-color="#13ce66" inactive-color="#ff4949"
                        :active-value="1" :inactive-value="0">
                    </el-switch>
                </el-form-item>
                
                <el-form-item label="标题" prop="title" :rules="[{ required: true, message: '标题不能为空' },]">
                    <el-input v-model="ruleForm.title"></el-input>
                </el-form-item>
    
                <el-form-item label="选择合同" :rules="[{ required: true, message: '文件不能为空' },]">
                    <el-upload 
                        @click.native="editUploadPng" 
                        class="upload-demo" drag :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload" 
                        action="/admin-api/guild/contract/editUpload"
                        :data="cachainData"
                        :limit="1" 
                        :file-list="fileList" 
                        :before-remove="beforeRemove" 
                        list-type="pdf">
                        <i class="el-icon-upload"></i>
                        <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
                        <div class="el-upload__tip" slot="tip">只能上传pdf文件,且不超过10M</div>
                    </el-upload>
                </el-form-item>
    
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')" v-html="status == 'edit' ? `更改` : `新增`">
                    </el-button>
                </el-form-item>
            </el-form>
        </div>
    </template>
    <script>
    export default {
        channel_name: 'Edit',
        filters: {},
        data() {
            return {
                regions: [],
                loading: false,
                regonShow: false,
                ruleForm: {
                    is_default: 0,
                },
                cachainData: {},
                fileList: []
            }
        },
    
        watch: {
            dialogVisible(val) {
                if (val) this.initData()
            }
        },
        mounted() {
            this.initData();
        },
        created() {
        },
        methods: {
            initData() {
    			this.fileList = [];
              	this.fileList.push({ 'name': '默认显示名', 'url': 'url地址' })          
            },
            editUploadPng() {
                this.cachainData = {
                    // id: this.ruleForm.id,
                    // platform_id: this.ruleForm.platform_id || 0,
                    // plugin_name: this.ruleForm.title,
                }
            },
            beforeAvatarUpload(file) {
                const isLt2M = file.size / 1024 / 1024 < 10;
                if (!isLt2M) {
                    this.$message.error('上传文件大小不能超过 10MB!');
                }
                // 截取上传文件的后缀名
                let fileType = file.name.substring(file.name.lastIndexOf(".") + 1);
                if (fileType !== 'pdf') {
                    this.$message.error('上传文件必须是pdf格式');
                }
                return isLt2M;
            },
            handleAvatarSuccess(res, file) {
                this.ruleForm.url = res.cndLocation;
            },
            beforeRemove(file, fileList) {
                // console.log(file)
                //pop方法是删除数值最后一个值
                // fileList.pop()
                this.ruleForm.url = '';
            }
        }
    }
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101

    结果

    在这里插入图片描述

  • 相关阅读:
    Java stream sorted使用 Comparator 进行多字段排序
    微信小程序开发整理-mp3
    【LeetCode刷题】--39.组合总和
    Mathematics-Vocabulary·数学专业英语词汇
    Mysql语法二:表的增删改查(简单查询)
    2023 柏鹭杯 --- Crypto fractrsa wp
    Swift 其他
    Docker的网络模式
    Mysql函数
    D-Wave推出新开源及解决无线信道解码新方案!
  • 原文地址:https://blog.csdn.net/qq_36303853/article/details/126244580