Upload組件上傳限制(寬诫欠、高涵卵、尺寸、格式)

iview-Upload組件官網鏈接:https://iview.github.io/components/upload

效果展示


css

.out-box{

width: 100%;

border: 1px solid #e6e6e6;

border-radius: 8px;

padding: 20px 20px 10px;

}

.img-border{

border: 1px solid #e6e6e6;

border-radius: 8px;

margin-right: 20px;

width:100px;

height:100px;

}

.upload-box{

width: 100px;

height:100px;

display: inline-block

}

.upload-inner-box{

width: 100px;

height: 100px;

font-size: 40px;

text-align: center;

background: #F5F5F5;

}

.upload-tip{

color:red

}

template

<div class="out-box">

<img class="img-border" v-if="seeView" :src="this.imageUrl"? />

<Upload ref="uploadFiles"

:show-upload-list="false"

:max-size="5120"

:on-success="handleSuccess"

:on-exceeded-size="handleMaxSize"

:before-upload="beforeUploadImg"

:loading="true"

type="drag"

:format="['jpg','jpeg','png','gif']"

:on-format-error="handleFormatError"

action="url"

class="upload-box">

? ? ? ? ? ? ? ? <div class="upload-inner-box">

? ? ? ? ? ? ? ? <div style="padding-top: 20px">+</div>

? ? ? ? ? ? ? ? <div style="font-size: 12px">請上傳圖片</div>

</div>

</Upload>

<div class="upload-tip">圖片大小勿超5M荒叼,尺寸為{{minWidth}}*{{minHeight}}轿偎,勿小于該尺寸,且盡量以該長寬比制圖以保證頁面效果</div>

</div>

data

seeView:"false",//是否展示已上傳的圖片

imageUrl:"",//上傳圖片的url

url:"",//上傳的地址

minWidth:number,//最小寬度

minHeight:number,//最小高度

methods

//上傳成功

? ? handleSuccess(response, file, fileList) {

? ? ? this.imageUrl = response.result;

? ? ? this.seeView = true;

? ? },

? ? //上傳的文件大小超出要求

? ? handleMaxSize() {

? ? ? this.$Modal.warning({

? ? ? ? title: "提示",

? ? ? ? content: "上傳縮略圖大小不能超過5M!!!",

? ? ? });

? ? },

? ? //上傳文件格式不符合要求

? ? handleFormatError() {

? ? ? this.$Modal.warning({

? ? ? ? title: "提示",

? ? ? ? content: "上傳縮略圖格式錯誤!!!",

? ? ? });

? ? },

? ? //上傳前對圖片寬高的檢驗

? ? beforeUploadImg(file) {

? ? ? this.checkImageWidth(file, minWidth).then((checkWidth) => {

? ? ? ? if (checkWidth) {

? ? ? ? ? this.checkImageHeight(file, minHeight).then((checkHeight) => {

? ? ? ? ? ? if (checkHeight) {

? ? ? ? ? ? ? this.$refs.uploadFiles.post(file);

? ? ? ? ? ? }

? ? ? ? ? });

? ? ? ? }

? ? ? });

? ? ? return false;

? ? },

? ? // 異步方法 檢驗圖片寬度

? ? async checkImageWidth(file, widthCheck) {

? ? ? let width = await this.getImageWidth(file);

? ? ? let checkWidth = width > widthCheck || width == widthCheck;

? ? ? if (!checkWidth) {

? ? ? ? this.$Notice.warning({

? ? ? ? ? title: "圖片寬度錯誤",

? ? ? ? ? desc:

? ? ? ? ? ? file.name +

? ? ? ? ? ? " 的寬度為" +

? ? ? ? ? ? width +

? ? ? ? ? ? "px, 請上傳寬度大于" +

? ? ? ? ? ? widthCheck +

? ? ? ? ? ? "px的圖片. ",

? ? ? ? });

? ? ? }

? ? ? return checkWidth;

? ? },

? ? // 獲取圖片寬度

? ? getImageWidth(file) {

? ? ? return new Promise((resolve) => {

? ? ? ? const reader = new FileReader();

? ? ? ? reader.readAsDataURL(file);

? ? ? ? reader.onload = function () {

? ? ? ? ? if (reader.readyState == 2) {

? ? ? ? ? ? const img = new Image();

? ? ? ? ? ? img.src = reader.result;

? ? ? ? ? ? img.onload = function () {

? ? ? ? ? ? ? resolve(this.width);

? ? ? ? ? ? };

? ? ? ? ? }

? ? ? ? };

? ? ? });

? ? },

? ? // 異步方法 檢驗圖片高度

? ? async checkImageHeight(file, heightCheck) {

? ? ? let height = await this.getImageHeight(file);

? ? ? let checkHeight = height > heightCheck || height == heightCheck;

? ? ? if (!checkHeight) {

? ? ? ? this.$Notice.warning({

? ? ? ? ? title: "圖片高度錯誤",

? ? ? ? ? desc:

? ? ? ? ? ? file.name +

? ? ? ? ? ? " 的高度為" +

? ? ? ? ? ? height +

? ? ? ? ? ? "px, 請上傳高度大于" +

? ? ? ? ? ? heightCheck +

? ? ? ? ? ? "px的圖片. ",

? ? ? ? });

? ? ? }

? ? ? return checkHeight;

? ? },

? ? // 獲取圖片寬度

? ? getImageHeight(file) {

? ? ? return new Promise((resolve) => {

? ? ? ? const reader = new FileReader();

? ? ? ? reader.readAsDataURL(file);

? ? ? ? reader.onload = function () {

? ? ? ? ? if (reader.readyState == 2) {

? ? ? ? ? ? const img = new Image();

? ? ? ? ? ? img.src = reader.result;

? ? ? ? ? ? img.onload = function () {

? ? ? ? ? ? ? resolve(this.height);

? ? ? ? ? ? };

? ? ? ? ? }

? ? ? ? };

? ? ? });

? ? },

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末被廓,一起剝皮案震驚了整個濱河市坏晦,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖昆婿,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件球碉,死亡現(xiàn)場離奇詭異,居然都是意外死亡仓蛆,警方通過查閱死者的電腦和手機睁冬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來多律,“玉大人痴突,你說我怎么就攤上這事±擒瘢” “怎么了辽装?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長相味。 經常有香客問我拾积,道長,這世上最難降的妖魔是什么丰涉? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任拓巧,我火速辦了婚禮,結果婚禮上一死,老公的妹妹穿的比我還像新娘肛度。我一直安慰自己,他們只是感情好投慈,可當我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布承耿。 她就那樣靜靜地躺著,像睡著了一般伪煤。 火紅的嫁衣襯著肌膚如雪加袋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天抱既,我揣著相機與錄音职烧,去河邊找鬼。 笑死防泵,一個胖子當著我的面吹牛蚀之,可吹牛的內容都是我干的。 我是一名探鬼主播捷泞,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼足删,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了肚邢?” 一聲冷哼從身側響起壹堰,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎骡湖,沒想到半個月后贱纠,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡响蕴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年谆焊,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片浦夷。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡辖试,死狀恐怖,靈堂內的尸體忽然破棺而出劈狐,到底是詐尸還是另有隱情罐孝,我是刑警寧澤,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布肥缔,位于F島的核電站莲兢,受9級特大地震影響,放射性物質發(fā)生泄漏续膳。R本人自食惡果不足惜改艇,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望坟岔。 院中可真熱鬧谒兄,春花似錦、人聲如沸社付。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽瘦穆。三九已至纪隙,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間扛或,已是汗流浹背绵咱。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留熙兔,地道東北人悲伶。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像住涉,于是被迫代替她去往敵國和親麸锉。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內容