vue+Element Ui 實現自動上傳圖片

點擊上傳文件按鈕 beforeAvatarUpload方法自動上傳1到多個圖片php

<el-upload
        class="upload-demo"
        ref="upload"
        accept=".jpg"
        multiple
        :data="radio"
        :action="actionUrl"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
         list-type="picture"
        :file-list="fileList"
        :before-upload="beforeAvatarUpload">
   <el-button slot="trigger" size="small"  :disabled="btnFlag" type="primary">上傳文件</el-button>   
</el-upload>

**//上傳畫稿以前先判斷畫稿規格 再插oracle 再post到php
//爲了解決onload異步使用promise**ajax

beforeAvatarUpload:function(file) {
    var _this = this;
    return new Promise(function(resolve, reject) { //爲了解決onload異步使用promise
        if(file.name.length > 100){
            _this.$alert(file.name+'圖片名稱字符長度超過100,不能上傳!', '提示', {confirmButtonText: '肯定'});
            reject();
            return;
        }
        var reader = new FileReader();
        var img_oracleFlag = false;
        reader.onload = function(event) { //onload是異步的
            var image = new Image();
            image.onload = function () {
                var width = this.width;
                var height = this.height;
                if (width>height && vm.radio.GGH=="小牌"){
                    _this.$alert(file.name+'不能上傳!', '提示', {confirmButtonText: '肯定'});
                    reject();
                    return;
                }else if (width<height && vm.radio.GGH=="大牌"){
                    _this.$alert(file.name+'不能上傳!', '提示', {confirmButtonText: '肯定'});
                    reject();
                    return;
                }else{
                    img_oracleFlag = true;
                    //插入oracle
                    $.ajax({ //必須使用同步ajax
                        url : OracleInterface,
                        type : 'post',
                        dataType : 'json',
                        async:false,
                        data : {},
                        success : function(res) {},
                        error : function(data) {},
                    })
                }
                resolve();
            };             
            image.src = event.target.result;
        }
        reader.readAsDataURL(file);
    });

    return true
},
相關文章
相關標籤/搜索