擼一個在vue中使用七牛上傳的組件

最近因為項目中用到了七牛上傳,因此封裝了個組件,以便提高效率。

注:七牛 JavaScript SDK使用指南官網(wǎng)

下載七牛并引入靜態(tài)資源

組件代碼

html部分

<template>
    <div class="upload">
        <div class="upload-pickfiles-lists" v-for="n in uploadurl.length">
            <div class="xg-upload-pickfiles">
                <img :src="uploadurl[n]">
            </div>
            <a @click="del($index)">刪除</a>
        </div>
        <div :id="container" class="upload-container">
            <div class="upload-pickfiles" :id="pickfiles" v-if="uploadurl.length < max">
            </div>
        </div>
    </div>
</template>

js部分

module.exports = {
        props:{
            // 最大上傳數(shù)量
            max:{
                type:Number,
                twoWay:true
            }, 
            // 返回的上傳地址
            uploadurl:{
                type: Array,
                twoWay:true,
            },
            // 拖拽區(qū)域
            container:{
                type: String,
                twoWay:false
            },
            // inputID
            pickfiles:{
                type: String,
                twoWay:false
            },
            // 多選上傳
            multiple:{
                type:Boolean,
                twoWay: false
            }
        },
        ready(){
            this.qiniu();
        },
        methods:{

            qiniu(){
                const self = this;
                const uploader = Qiniu.uploader({
                     runtimes: 'html5,flash,html4',    //上傳模式,依次退化
                            browse_button: self.pickfiles,       //上傳選擇的點選按鈕,**必需**
                            uptoken_url: '/token',            //Ajax請求upToken的Url,**強(qiáng)烈建議設(shè)置**(服務(wù)端提供)
                            // uptoken : '', //若未指定uptoken_url,則必須指定 uptoken ,uptoken由其他程序生成
                            // unique_names: true, // 默認(rèn) false,key為文件名。若開啟該選項,SDK為自動生成上傳成功后的key(文件名)。
                            // save_key: true,   // 默認(rèn) false。若在服務(wù)端生成uptoken的上傳策略中指定了 `sava_key`,則開啟,SDK會忽略對key的處理
                            domain: 'http://qiniu-plupload.qiniudn.com/',   //bucket 域名,下載資源時用到,**必需**
                            get_new_uptoken: false,  //設(shè)置上傳文件的時候是否每次都重新獲取新的token
                            container: self.container,           //上傳區(qū)域DOM ID,默認(rèn)是browser_button的父元素,
                            max_file_size: '100mb',           //最大文件體積限制
                            flash_swf_url: 'js/plupload/Moxie.swf',  //引入flash,相對路徑
                            max_retries: 3,                   //上傳失敗最大重試次數(shù)
                            dragdrop: true,                   //開啟可拖曳上傳
                            drop_element: 'container',        //拖曳上傳區(qū)域元素的ID,拖曳文件或文件夾后可觸發(fā)上傳
                            chunk_size: '4mb',                //分塊上傳時,每片的體積
                            auto_start: true,                 //選擇文件后自動上傳,若關(guān)閉需要自己綁定事件觸發(fā)上傳
                            multi_selection:self.multiple,   //多選上傳
                    filters : {
                        max_file_size : '100mb',
                        prevent_duplicates: true,
                        mime_types: [
                            { title : 'Image files', extensions : 'JPEG,jpg,png' }, // 限定jpg,gif,png后綴上傳
                            ]
                        },
                        init: {
                        // 'FilesAdded': function (up, files) {
                        //     $('table').show();
                        //     $('#success').hide();
                        //     plupload.each(files, function (file) {
                        //     });
                        // },
                        // BeforeUpload(up, file) {
                        // },
                        // 'UploadProgress': function (up, file) {

                        // },
                        // 'UploadComplete': function () {

                        // },
                        FileUploaded(up, file, info) {
                            const domain = up.getOption('domain');
                            const res = JSON.parse(info);
                            const sourceLink = domain + res.key;
                            self.uploadurl.push(sourceLink);
                            console.info(self.uploadurl);
                        },
                        Error(up, err, errTip) {
                            // $('table').show();
                            // const progress = new FileProgress(err.file, 'fsUploadProgress');
                            // progress.setError();
                            // progress.setStatus(errTip);
                        }
                    }
                });
            },
            del(index){
                this.uploadurl.splice(index, 1);
            }
        }
    };

使用組件

import upload from './../components/upload.vue';
components: {
      upload,
    },
<upload :uploadurl="uploadimgs" :pickfile ="pickfiles" : multiple="true">
                <div slot="upload">
                  <div :id="pickfiles" class="btn">點擊添加門店logo/照片<img :src="uploadimgs" class="uploadimg"></div>
                </div>
              </upload>

這樣就可以愉快地上傳了

  • 如有遺漏或不恰之處,敬請見諒并指出
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)載請注明原文地址: http://www.itdecent.cn/p/6742c4746421 先丟兩個官方文檔...
    逗比二二二閱讀 3,583評論 0 1
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,319評論 25 708
  • 四組,隸屬于昭通學(xué)院附中66班,組員共6人,胖瘦不同,高矮不齊,各具特色。據(jù)賈雄所述,本組是“宇宙無敵超級帥組”。...
    煎餅小臭狗閱讀 541評論 2 5
  • “生命如蓮次第開”是我微信上的個性簽名,也是我最鐘愛的一句話。一個人在世間行走,總是在尋找自己,這一輩子會去見...
    居里葉閱讀 461評論 0 6
  • 文/楊海濤(潛能訓(xùn)練導(dǎo)師) 英國古典經(jīng)濟(jì)學(xué)家邊沁認(rèn)為,國民最大化幸福與快樂是人類的終極關(guān)懷,是人類追求的終極價值目...
    釋放能量SFNL88閱讀 446評論 0 0

友情鏈接更多精彩內(nèi)容