Commit a6ac0a76032f205ccbf63e8da674e601a9e5771d
1 parent
bc3bcb09
update Upload
update Upload
Showing
3 changed files
with
133 additions
and
64 deletions
Show diff stats
src/components/upload/upload.vue
... | ... | @@ -202,6 +202,24 @@ |
202 | 202 | } |
203 | 203 | }, |
204 | 204 | post (file) { |
205 | + // check format | |
206 | + if (this.format.length) { | |
207 | + const _file_format = file.name.split('.').pop().toLocaleLowerCase(); | |
208 | + const checked = this.format.some(item => item.toLocaleLowerCase() === _file_format); | |
209 | + if (!checked) { | |
210 | + this.onFormatError(file); | |
211 | + return false; | |
212 | + } | |
213 | + } | |
214 | + | |
215 | + // check maxSize | |
216 | + if (this.maxSize) { | |
217 | + if (file.size > this.maxSize * 1024) { | |
218 | + this.onExceededSize(file); | |
219 | + return false; | |
220 | + } | |
221 | + } | |
222 | + | |
205 | 223 | this.handleStart(file); |
206 | 224 | let formData = new FormData(); |
207 | 225 | formData.append(this.name, file); |
... | ... | @@ -235,24 +253,6 @@ |
235 | 253 | showProgress: true |
236 | 254 | }; |
237 | 255 | |
238 | - // check format | |
239 | - if (this.format.length) { | |
240 | - const _file_format = _file.name.split('.').pop().toLocaleLowerCase(); | |
241 | - const checked = this.format.some(item => item.toLocaleLowerCase() === _file_format); | |
242 | - if (checked) { | |
243 | - this.onFormatError(file); | |
244 | - return; | |
245 | - } | |
246 | - } | |
247 | - | |
248 | - // check maxSize | |
249 | - if (this.maxSize) { | |
250 | - if (_file.size > this.maxSize * 1024) { | |
251 | - this.onExceededSize(file); | |
252 | - return; | |
253 | - } | |
254 | - } | |
255 | - | |
256 | 256 | this.fileList.push(_file); |
257 | 257 | }, |
258 | 258 | getFile (file) { | ... | ... |
src/styles/components/upload.less
... | ... | @@ -16,7 +16,7 @@ |
16 | 16 | overflow: hidden; |
17 | 17 | position: relative; |
18 | 18 | |
19 | - & + span{ | |
19 | + & > span{ | |
20 | 20 | cursor: pointer; |
21 | 21 | transition: color @transition-time @ease-in-out; |
22 | 22 | i{ |
... | ... | @@ -30,7 +30,7 @@ |
30 | 30 | |
31 | 31 | &:hover{ |
32 | 32 | background: @input-disabled-bg; |
33 | - & + span{ | |
33 | + & > span{ | |
34 | 34 | color: @primary-color; |
35 | 35 | i{ |
36 | 36 | color: @text-color; | ... | ... |
test/routers/upload.vue
1 | 1 | <template> |
2 | - <div style="margin: 50px;width: 300px;"> | |
3 | - <Upload | |
4 | - action="//jsonplaceholder.typicode.com/posts/" | |
5 | - multiple | |
6 | - type="drag" | |
7 | - :max-size="10000" | |
8 | - :default-file-list="defaultFileList"> | |
9 | - <div style="padding: 40px 100px"> | |
10 | - <i-button>上传</i-button> | |
2 | + <div class="demo-upload-list" v-for="item in uploadList"> | |
3 | + <template v-if="item.status === 'finished'"> | |
4 | + <img :src="item.url"> | |
5 | + <div class="demo-upload-list-cover"> | |
6 | + <Icon type="ios-eye-outline" @click="handleView(item.name)"></Icon> | |
7 | + <Icon type="ios-trash-outline" @click="handleRemove(item)"></Icon> | |
11 | 8 | </div> |
12 | - <!--<div slot="tip">只能上传 jpg/png 格式的文件</div>--> | |
13 | - </Upload> | |
9 | + </template> | |
10 | + <template v-else> | |
11 | + <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress> | |
12 | + </template> | |
14 | 13 | </div> |
14 | + <Upload | |
15 | + v-ref:upload | |
16 | + :show-upload-list="false" | |
17 | + :default-file-list="defaultList" | |
18 | + :on-success="handleSuccess" | |
19 | + :format="['jpg','jpeg','png']" | |
20 | + :max-size="2048" | |
21 | + :on-format-error="handleFormatError" | |
22 | + :on-exceeded-size="handleMaxSize" | |
23 | + multiple | |
24 | + type="drag" | |
25 | + action="//jsonplaceholder.typicode.com/posts/" | |
26 | + style="display: inline-block;width:58px;"> | |
27 | + <div style="width: 58px;height:58px;line-height: 58px;"> | |
28 | + <Icon type="camera" size="20"></Icon> | |
29 | + </div> | |
30 | + </Upload> | |
31 | + <Modal title="查看图片" :visible.sync="visible"> | |
32 | + <img :src="'https://o5wwk8baw.qnssl.com/' + imgName + '/large'" v-if="visible" style="width: 100%"> | |
33 | + </Modal> | |
15 | 34 | </template> |
16 | 35 | <script> |
17 | 36 | export default { |
18 | - props: {}, | |
19 | 37 | data () { |
20 | 38 | return { |
21 | - defaultFileList: [ | |
22 | - { | |
23 | - 'name': 'Vue.js权威指南.zip', | |
24 | - 'url': 'http://www.baidu.com' | |
25 | - }, | |
26 | - { | |
27 | - 'name': 'Vue.js权威指南.doc', | |
28 | - 'url': 'http://www.baidu.com' | |
29 | - }, | |
30 | - { | |
31 | - 'name': '套马的汉子.mp3', | |
32 | - 'url': 'http://www.baidu.com' | |
33 | - }, | |
34 | - { | |
35 | - 'name': '风景.jpg', | |
36 | - 'url': 'http://www.baidu.com' | |
37 | - }, | |
39 | + defaultList: [ | |
38 | 40 | { |
39 | - 'name': 'Vue.js权威指南.mp4', | |
40 | - 'url': 'http://www.baidu.com' | |
41 | + 'name': 'a42bdcc1178e62b4694c830f028db5c0', | |
42 | + 'url': 'https://o5wwk8baw.qnssl.com/a42bdcc1178e62b4694c830f028db5c0/avatar' | |
41 | 43 | }, |
42 | 44 | { |
43 | - 'name': '套马的汉子.csv', | |
44 | - 'url': 'http://www.baidu.com' | |
45 | - }, | |
46 | - { | |
47 | - 'name': '风景.ppt', | |
48 | - 'url': 'http://www.baidu.com' | |
49 | - }, | |
50 | - ] | |
51 | - }; | |
45 | + 'name': 'bc7521e033abdd1e92222d733590f104', | |
46 | + 'url': 'https://o5wwk8baw.qnssl.com/bc7521e033abdd1e92222d733590f104/avatar' | |
47 | + } | |
48 | + ], | |
49 | + imgName: '', | |
50 | + visible: false | |
51 | + } | |
52 | + }, | |
53 | + computed: { | |
54 | + uploadList () { | |
55 | + return this.$refs.upload ? this.$refs.upload.fileList : []; | |
56 | + } | |
52 | 57 | }, |
53 | - computed: {}, | |
54 | - methods: {} | |
55 | - }; | |
56 | -</script> | |
57 | 58 | \ No newline at end of file |
59 | + methods: { | |
60 | + handleView (name) { | |
61 | + this.imgName = name; | |
62 | + this.visible = true; | |
63 | + }, | |
64 | + handleRemove (file) { | |
65 | + // 从 upload 实例删除数据 | |
66 | + const fileList = this.$refs.upload.fileList; | |
67 | + this.$refs.upload.fileList.splice(fileList.indexOf(file), 1); | |
68 | + }, | |
69 | + handleSuccess (res, file) { | |
70 | + // 因为上传过程为实例,这里模拟添加 url | |
71 | + file.url = 'https://o5wwk8baw.qnssl.com/7eb99afb9d5f317c912f08b5212fd69a/avatar'; | |
72 | + file.name = '7eb99afb9d5f317c912f08b5212fd69a'; | |
73 | + }, | |
74 | + handleFormatError (file) { | |
75 | + this.$Notice.warning({ | |
76 | + title: '文件格式不正确', | |
77 | + desc: '文件 ' + file.name + ' 格式不正确,请上传 jpg 或 png 格式的图片。' | |
78 | + }); | |
79 | + }, | |
80 | + handleMaxSize (file) { | |
81 | + this.$Notice.warning({ | |
82 | + title: '超出文件大小限制', | |
83 | + desc: '文件 ' + file.name + ' 太大,不能超过 2M。' | |
84 | + }); | |
85 | + } | |
86 | + } | |
87 | + } | |
88 | +</script> | |
89 | +<style> | |
90 | + .demo-upload-list{ | |
91 | + display: inline-block; | |
92 | + width: 60px; | |
93 | + height: 60px; | |
94 | + text-align: center; | |
95 | + line-height: 60px; | |
96 | + border: 1px solid transparent; | |
97 | + border-radius: 4px; | |
98 | + overflow: hidden; | |
99 | + background: #fff; | |
100 | + position: relative; | |
101 | + box-shadow: 0 1px 1px rgba(0,0,0,.2); | |
102 | + margin-right: 4px; | |
103 | + } | |
104 | + .demo-upload-list img{ | |
105 | + width: 100%; | |
106 | + height: 100%; | |
107 | + } | |
108 | + .demo-upload-list-cover{ | |
109 | + display: none; | |
110 | + position: absolute; | |
111 | + top: 0; | |
112 | + bottom: 0; | |
113 | + left: 0; | |
114 | + right: 0; | |
115 | + background: rgba(0,0,0,.6); | |
116 | + } | |
117 | + .demo-upload-list:hover .demo-upload-list-cover{ | |
118 | + display: block; | |
119 | + } | |
120 | + .demo-upload-list-cover i{ | |
121 | + color: #fff; | |
122 | + font-size: 20px; | |
123 | + cursor: pointer; | |
124 | + margin: 0 2px; | |
125 | + } | |
126 | +</style> | ... | ... |