vue+ts+vuetify vue-upload-component 拖拽上傳圖片

安裝

npm i vue-upload-component

使用

<template>
    <div class="example-drag">
        <div :class="type === 'small' ? 'upload-small' : 'upload-big'">
            <div class="example-btn">
                <file-upload
                    class="btn btn-primary"
                    post-action="/upload/post"
                    :multiple="true"
                    :drop="true"
                    :drop-directory="true"
                    :size="size"
                    @input-filter="inputFilter"
                    v-model="files"
                    ref="upload"
                >
                    <div
                        class="mx-auto"
                        :class="type === 'small' ? 'my-3' : 'mt-14'"
                    >
                        <v-icon
                            :class="type === 'small' ? 'mb-5' : 'mb-10'"
                            size="35"
                            color="#3073E1"
                        >
                            mdi-inbox
                        </v-icon>
                        <p :class="type === 'small' ? 'mb-2' : 'mb-6'">
                            ここにファイルをドロップまたは
                        </p>
                        <v-btn color="primary" height="40"
                            >ファイルを選択</v-btn
                        >
                    </div>
                </file-upload>
            </div>
        </div>
    </div>
</template>

<script lang="ts">
import { Component, Vue, Prop, Emit, Watch } from 'vue-property-decorator';
import FileUpload from 'vue-upload-component';
import imgDiv from '~/components/pageCom/imgDiv.vue';

@Component({
    name: 'dragUpload',
    components: {
        FileUpload,
        imgDiv
    }
})
export default class DragUpload extends Vue {
    @Prop({ type: String, default: 'small', required: false })
    private type: any;
    @Prop({ type: Number, default: 0, required: false })
    private size: any;
    @Watch('files')
    do() {
        this.returnList();
    }
    @Emit('getList')
    returnList() {
        return this.files;
    }
    mounted() {
        console.log(this.type);
    }
    private files: any = [];

    public getData() {
        return this.files;
    }

    private inputFilter(
        newFile: { name: string; file: any; url: string },
        oldFile: { file: any },
        prevent: () => any
    ) {
        if (newFile && !oldFile) {
            if (!/\.(gif|jpg|jpeg|png|webp)$/i.test(newFile.name)) {
                console.log('文件格式不對(duì)');
                return prevent();
            }
            if (this.size && newFile.file.size > this.size) {
                console.log('文件超大');
                return prevent();
            }
        }
        if (newFile && (!oldFile || newFile.file !== oldFile.file)) {
            newFile.url = '';
            let URL = window.URL || window.webkitURL;

            if (URL && URL.createObjectURL) {
                newFile.url = URL.createObjectURL(newFile.file);
            }
        }
    }
}
</script>
<style lang="scss" scoped>
.example-drag .upload-small {
    width: 300px;
    height: 150px;
    background-color: #fafafa;
    text-align: center;
    border: 1px solid #d9d9d9;
}
.example-drag .upload-big {
    width: 100%;
    height: 281px;
    background-color: #fafafa;
    text-align: center;
    border: 1px solid #d9d9d9;
}
.example-drag .drop-active {
    top: 0;
    bottom: 0;
    right: 0;
    left: 0;
    position: fixed;
    z-index: 9999;
    opacity: 0.6;
    text-align: center;
    background: #000;
}
.example-drag .drop-active h3 {
    margin: -0.5em 0 0;
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
    font-size: 40px;
    color: #fff;
    padding: 0;
}
.mx-auto {
    text-align: center;
}
</style>

效果圖

拖拽上傳.jpg

自己封裝回顯圖片

<template>
    <div class="row-min-height">
        <div
            class="row-inline mr-5"
            v-for="(item, index) in imgList"
            :key="index"
        >
            <div class="m-a-6 mr-1">
                <img class="w-h-48" :src="item.src || item.url" />
            </div>
            <div>
                <p class="imgName mt-2 mb-1">{{ item.name }}</p>
                <p class="imgSize">{{ washSize(item.size) }}</p>
            </div>
            <div class="fl-r-m-17 pt-5">
                <v-icon @click="deleteImg(index)">mdi-close</v-icon>
            </div>
        </div>
    </div>
</template>

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import { renderSize } from '~/utils/tool';
@Component({
    name: 'imgDiv'
})
export default class imgDiv extends Vue {
    @Prop({ type: Array, default: [], required: true })
    private imgList: any;

    private deleteImg(index: number) {
        this.imgList.splice(index, 1);
    }
    private washSize(size: string) {
        return renderSize(size);
    }
}
</script>
<style lang="scss" scoped>
.row-inline {
    display: inline-block;
    width: 308px;
    height: 60px;
    background-color: #fafafa;
}
.row-inline div {
    vertical-align: middle;
    display: inline-block;
}

.imgName {
    font-family: Noto Sans CJK JP;
    font-style: normal;
    font-weight: normal;
    font-size: 14px;
    line-height: 22px;
    color: #212121;
}
.imgSize {
    font-family: Noto Sans CJK JP;
    font-style: normal;
    font-weight: 500;
    font-size: 10px;
    line-height: 16px;
    color: #9e9e9e;
}
.row-min-height {
    min-height: 64px;
}
</style>

效果圖

圖片回顯.jpg

文件大小格式化

export function renderSize(value: string) {
    if (!value || value === '') {
        return '0 Bytes';
    }
    const unitArr = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

    let index = 0;
    const srcsize = parseFloat(value);

    index = Math.floor(Math.log(srcsize) / Math.log(1024));
    const size = srcsize / Math.pow(1024, index);

    const res = size.toFixed(2);

    return res + unitArr[index];
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末瞒大,一起剝皮案震驚了整個(gè)濱河市咪奖,隨后出現(xiàn)的幾起案子饮寞,更是在濱河造成了極大的恐慌箱舞,老刑警劉巖揍诽,帶你破解...
    沈念sama閱讀 221,888評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件娜膘,死亡現(xiàn)場(chǎng)離奇詭異硼瓣,居然都是意外死亡瞳抓,警方通過(guò)查閱死者的電腦和手機(jī)刺啦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,677評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門留特,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事蜕青」兜福” “怎么了?”我有些...
    開封第一講書人閱讀 168,386評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵右核,是天一觀的道長(zhǎng)慧脱。 經(jīng)常有香客問我,道長(zhǎng)贺喝,這世上最難降的妖魔是什么菱鸥? 我笑而不...
    開封第一講書人閱讀 59,726評(píng)論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮躏鱼,結(jié)果婚禮上氮采,老公的妹妹穿的比我還像新娘。我一直安慰自己染苛,他們只是感情好鹊漠,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,729評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著茶行,像睡著了一般躯概。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上畔师,一...
    開封第一講書人閱讀 52,337評(píng)論 1 310
  • 那天娶靡,我揣著相機(jī)與錄音,去河邊找鬼茉唉。 笑死固蛾,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的度陆。 我是一名探鬼主播艾凯,決...
    沈念sama閱讀 40,902評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼懂傀!你這毒婦竟也來(lái)了趾诗?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,807評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤蹬蚁,失蹤者是張志新(化名)和其女友劉穎恃泪,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體犀斋,經(jīng)...
    沈念sama閱讀 46,349評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡贝乎,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,439評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了叽粹。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片览效。...
    茶點(diǎn)故事閱讀 40,567評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡却舀,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出锤灿,到底是詐尸還是另有隱情挽拔,我是刑警寧澤,帶...
    沈念sama閱讀 36,242評(píng)論 5 350
  • 正文 年R本政府宣布但校,位于F島的核電站螃诅,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏状囱。R本人自食惡果不足惜术裸,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,933評(píng)論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望浪箭。 院中可真熱鬧穗椅,春花似錦、人聲如沸奶栖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,420評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)宣鄙。三九已至袍镀,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間冻晤,已是汗流浹背苇羡。 一陣腳步聲響...
    開封第一講書人閱讀 33,531評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留鼻弧,地道東北人设江。 一個(gè)月前我還...
    沈念sama閱讀 48,995評(píng)論 3 377
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像攘轩,于是被迫代替她去往敵國(guó)和親叉存。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,585評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容