Skip to content

Commit b782c1f

Browse files
committed
修改上传组件的配置
1 parent 98fc2d0 commit b782c1f

File tree

2 files changed

+59
-56
lines changed

2 files changed

+59
-56
lines changed

src/components/File/Upload.vue

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<el-tabs v-model="activeName">
1818
<el-tab-pane label="本地上传" name="localhost">
1919
<div class="upload-content">
20-
<span class="text-muted">只能上传{{ cheekConfig.ext }}文件,且大小不超过{{ cheekConfig.size | renderSize }},且宽高 {{ cheekConfig.width ? cheekConfig.width + 'px' : "不限" }} * {{ cheekConfig.height ? cheekConfig.height + 'px' : "不限" }}</span>
20+
<span class="text-muted">只能上传{{ ext }}文件,且大小不超过{{ size | renderSize }},且宽高 {{ width ? width + 'px' : "不限" }} * {{ height ? height + 'px' : "不限" }}</span>
2121
<br>
2222
<div class="widget-upload" @dragenter="onDrag" @dragover="onDrag" @drop="onDrop">
2323
<input type="file" ref="upload" name="file" class="widget-upload__file" @change="onChange">
@@ -33,7 +33,7 @@
3333
</el-tab-pane>
3434
<el-tab-pane label="远程地址获取" name="network">
3535
<div class="upload-content">
36-
<span class="text-muted">只能上传{{ cheekConfig.ext }}文件,且大小不超过{{ cheekConfig.size | renderSize }},且宽高 {{ cheekConfig.width ? cheekConfig.width + 'px' : "不限" }} * {{ cheekConfig.height ? cheekConfig.height + 'px' : "不限" }}</span>
36+
<span class="text-muted">只能上传{{ ext }}文件,且大小不超过{{ size | renderSize }},且宽高 {{ width ? width + 'px' : "不限" }} * {{ height ? height + 'px' : "不限" }}</span>
3737
<br>
3838
<div class="widget-upload">
3939
<el-input
@@ -63,16 +63,17 @@ import { renderSize } from "../../filtres/index";
6363
export default {
6464
name: "Upload",
6565
props: {
66-
cheekConfig: {
67-
type: Object,
68-
default: function() {
69-
return {
70-
size: 6000, // 文件大小
71-
ext: "jpg,png,gif", // 文件后缀
72-
width: "", // 限制宽度
73-
height: "" // 限制高度
74-
};
75-
}
66+
height: {
67+
type: Number
68+
},
69+
width: {
70+
type: Number
71+
},
72+
ext: {
73+
type: String
74+
},
75+
size: {
76+
type: Number
7677
},
7778
color: {
7879
type: String,
@@ -192,23 +193,25 @@ export default {
192193
},
193194
beforeUpload(file) {
194195
const name = file.name ? file.name : "";
195-
const ext = name
196+
let ext = name
196197
? name.substr(name.lastIndexOf(".") + 1, name.length)
197198
: true;
199+
// 转成小写
200+
ext = ext.toLowerCase();
198201
let isExt = false;
199202
// 如果有坚持文件后缀的配置
200-
if (this.cheekConfig.ext) {
201-
isExt = this.cheekConfig.ext.indexOf(ext) >= 0;
203+
if (this.ext) {
204+
isExt = this.ext.indexOf(ext) >= 0;
202205
if (!isExt) {
203206
this.$message.error(
204-
"文件只能为 " + this.cheekConfig.ext + " 格式!"
207+
"文件只能为 " + this.ext + " 格式!"
205208
);
206209
return false;
207210
}
208211
}
209212
let isSize = false;
210-
if (this.cheekConfig.size) {
211-
let sizeStr = this.cheekConfig.size;
213+
if (this.size) {
214+
let sizeStr = this.size;
212215
isSize = sizeStr > 0 && file.size > sizeStr;
213216
if (!isSize) {
214217
this.$message.error(
@@ -218,10 +221,10 @@ export default {
218221
}
219222
}
220223
const _this = this;
221-
if (_this.cheekConfig.width || _this.cheekConfig.height) {
224+
if (_this.width || _this.height) {
222225
return new Promise(function(resolve, reject) {
223-
let width = _this.cheekConfig.width;
224-
let height = _this.cheekConfig.height;
226+
let width = _this.width;
227+
let height = _this.height;
225228
let _URL = window.URL || window.webkitURL;
226229
let img = new Image();
227230
img.src = _URL.createObjectURL(file);
@@ -261,39 +264,39 @@ export default {
261264
</script>
262265

263266
<style type="text/scss" lang="scss">
264-
.upload-dialog__body {
265-
.el-dialog__header {
266-
background-color: #f3f3f3;
267-
border-top-left-radius: 6px;
268-
border-top-right-radius: 6px;
269-
}
270-
.el-dialog__body {
271-
padding: 12px 20px !important;
272-
}
273-
}
274-
.upload-content {
275-
padding-top: 15px;
276-
.widget-upload {
277-
position: relative;
278-
}
279-
.widget-upload__text {
280-
width: 66.66%;
281-
padding-right: 15px;
282-
.el-input__inner {
283-
border: 1px solid #ccc;
284-
background-color: #eee;
267+
.upload-dialog__body {
268+
.el-dialog__header {
269+
background-color: #f3f3f3;
270+
border-top-left-radius: 6px;
271+
border-top-right-radius: 6px;
272+
}
273+
.el-dialog__body {
274+
padding: 12px 20px !important;
285275
}
286276
}
287-
.widget-upload {
288-
padding-top: 5px;
289-
.widget-upload__file {
290-
position: absolute;
291-
opacity: 0;
292-
width: 85%;
293-
height: 100%;
294-
z-index: 10;
295-
cursor: pointer;
277+
.upload-content {
278+
padding-top: 15px;
279+
.widget-upload {
280+
position: relative;
281+
}
282+
.widget-upload__text {
283+
width: 66.66%;
284+
padding-right: 15px;
285+
.el-input__inner {
286+
border: 1px solid #ccc;
287+
background-color: #eee;
288+
}
289+
}
290+
.widget-upload {
291+
padding-top: 5px;
292+
.widget-upload__file {
293+
position: absolute;
294+
opacity: 0;
295+
width: 85%;
296+
height: 100%;
297+
z-index: 10;
298+
cursor: pointer;
299+
}
296300
}
297301
}
298-
}
299302
</style>

src/views/components/upload-demo.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<el-card class="box-card w100" v-if="filePathUrl1">
77
<img :src="filePathUrl1" style="width: 100%;" alt="">
88
</el-card>
9-
<upload :cheekConfig="{ext: 'jpg', width: 100}" @on-select="onSelect1"></upload>
9+
<upload ext="jpg" :width="100" @on-select="onSelect1"></upload>
1010
</el-card>
1111
</div>
1212
<div class="page">
@@ -15,7 +15,7 @@
1515
<el-card class="box-card w100" v-if="filePathUrl2">
1616
<video :src="filePathUrl2" controls="controls" style="width: 100%;"></video>
1717
</el-card>
18-
<upload :cheekConfig="{ext: 'mp4'}" @on-select="onSelect2"></upload>
18+
<upload ext="mp4" @on-select="onSelect2"></upload>
1919
</el-card>
2020
</div>
2121
<div class="page">
@@ -24,7 +24,7 @@
2424
<el-card class="box-card w100" v-if="filePathUrl3">
2525
<audio :src="filePathUrl3" controls="controls" style="width: 100%;"></audio>
2626
</el-card>
27-
<upload :cheekConfig="{ext: 'mp3'}" @on-select="onSelect3"></upload>
27+
<upload ext="mp3" @on-select="onSelect3"></upload>
2828
</el-card>
2929
</div>
3030
<div class="page">
@@ -33,7 +33,7 @@
3333
<el-card class="box-card" v-if="filePathUrl4">
3434
文件地址:{{ filePathUrl4 }}
3535
</el-card>
36-
<upload :cheekConfig="{size: 60000}" @on-select="onSelect4"></upload>
36+
<upload :size="60000" @on-select="onSelect4"></upload>
3737
</el-card>
3838
</div>
3939
</div>

0 commit comments

Comments
 (0)