Blame view

src/components/upload/upload.vue 10.4 KB
3affd6f3   梁灏   init upload compo...
1
  <template>
ddef2bb3   梁灏   update Upload
2
3
4
5
6
      <div :class="[prefixCls]">
          <div
              :class="classes"
              @click="handleClick"
              @drop.prevent="onDrop"
929d4915   梁灏   Upload add paste ...
7
              @paste="handlePaste"
ddef2bb3   梁灏   update Upload
8
9
10
              @dragover.prevent="dragOver = true"
              @dragleave.prevent="dragOver = false">
              <input
5d08ddf2   梁灏   support Progress ...
11
                  ref="input"
ddef2bb3   梁灏   update Upload
12
                  type="file"
5d08ddf2   梁灏   support Progress ...
13
                  :class="[prefixCls + '-input']"
ddef2bb3   梁灏   update Upload
14
15
16
17
18
19
20
21
22
23
24
                  @change="handleChange"
                  :multiple="multiple"
                  :accept="accept">
              <slot></slot>
          </div>
          <slot name="tip"></slot>
          <upload-list
              v-if="showUploadList"
              :files="fileList"
              @on-file-remove="handleRemove"
              @on-file-preview="handlePreview"></upload-list>
9671827f   梁灏   update Upload
25
      </div>
3affd6f3   梁灏   init upload compo...
26
27
  </template>
  <script>
ddef2bb3   梁灏   update Upload
28
      import UploadList from './upload-list.vue';
9671827f   梁灏   update Upload
29
30
      import ajax from './ajax';
      import { oneOf } from '../../utils/assist';
cd78c9c4   梁灏   some comps suppor...
31
      import Emitter from '../../mixins/emitter';
9671827f   梁灏   update Upload
32
33
34
  
      const prefixCls = 'ivu-upload';
  
3affd6f3   梁灏   init upload compo...
35
      export default {
34ee7b4a   梁灏   support Tree & ad...
36
          name: 'Upload',
cd78c9c4   梁灏   some comps suppor...
37
          mixins: [ Emitter ],
ddef2bb3   梁灏   update Upload
38
          components: { UploadList },
9671827f   梁灏   update Upload
39
40
41
42
43
44
45
46
          props: {
              action: {
                  type: String,
                  required: true
              },
              headers: {
                  type: Object,
                  default () {
7f0b9f3a   梁灏   update Upload
47
                      return {};
9671827f   梁灏   update Upload
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
                  }
              },
              multiple: {
                  type: Boolean,
                  default: false
              },
              data: {
                  type: Object
              },
              name: {
                  type: String,
                  default: 'file'
              },
              withCredentials: {
                  type: Boolean,
                  default: false
              },
              showUploadList: {
                  type: Boolean,
                  default: true
              },
              type: {
                  type: String,
                  validator (value) {
                      return oneOf(value, ['select', 'drag']);
ddef2bb3   梁灏   update Upload
73
74
                  },
                  default: 'select'
9671827f   梁灏   update Upload
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
              },
              format: {
                  type: Array,
                  default () {
                      return [];
                  }
              },
              accept: {
                  type: String
              },
              maxSize: {
                  type: Number
              },
              beforeUpload: Function,
              onProgress: {
                  type: Function,
                  default () {
                      return {};
                  }
              },
              onSuccess: {
                  type: Function,
                  default () {
                      return {};
                  }
              },
              onError: {
                  type: Function,
                  default () {
                      return {};
                  }
              },
              onRemove: {
                  type: Function,
                  default () {
                      return {};
                  }
              },
              onPreview: {
                  type: Function,
                  default () {
                      return {};
                  }
              },
              onExceededSize: {
                  type: Function,
                  default () {
                      return {};
                  }
              },
              onFormatError: {
                  type: Function,
                  default () {
                      return {};
                  }
              },
              defaultFileList: {
                  type: Array,
                  default() {
                      return [];
                  }
929d4915   梁灏   Upload add paste ...
136
137
138
139
              },
              paste: {
                  type: Boolean,
                  default: false
a367d3e4   梁灏   fixed #5171
140
141
142
143
              },
              disabled: {
                  type: Boolean,
                  default: false
9671827f   梁灏   update Upload
144
145
              }
          },
3affd6f3   梁灏   init upload compo...
146
          data () {
9671827f   梁灏   update Upload
147
148
149
150
151
152
153
154
155
156
157
158
              return {
                  prefixCls: prefixCls,
                  dragOver: false,
                  fileList: [],
                  tempIndex: 1
              };
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
ddef2bb3   梁灏   update Upload
159
                          [`${prefixCls}-select`]: this.type === 'select',
9671827f   梁灏   update Upload
160
                          [`${prefixCls}-drag`]: this.type === 'drag',
bc3bcb09   梁灏   update Upload
161
                          [`${prefixCls}-dragOver`]: this.type === 'drag' && this.dragOver
9671827f   梁灏   update Upload
162
163
164
165
166
167
168
                      }
                  ];
              },
  
          },
          methods: {
              handleClick () {
a367d3e4   梁灏   fixed #5171
169
                  if (this.disabled) return;
5d08ddf2   梁灏   support Progress ...
170
                  this.$refs.input.click();
9671827f   梁灏   update Upload
171
172
173
174
175
176
177
178
              },
              handleChange (e) {
                  const files = e.target.files;
  
                  if (!files) {
                      return;
                  }
                  this.uploadFiles(files);
5d08ddf2   梁灏   support Progress ...
179
                  this.$refs.input.value = null;
9671827f   梁灏   update Upload
180
181
182
              },
              onDrop (e) {
                  this.dragOver = false;
a367d3e4   梁灏   fixed #5171
183
                  if (this.disabled) return;
9671827f   梁灏   update Upload
184
185
                  this.uploadFiles(e.dataTransfer.files);
              },
929d4915   梁灏   Upload add paste ...
186
              handlePaste (e) {
a367d3e4   梁灏   fixed #5171
187
                  if (this.disabled) return;
929d4915   梁灏   Upload add paste ...
188
189
190
191
                  if (this.paste) {
                      this.uploadFiles(e.clipboardData.files);
                  }
              },
9671827f   梁灏   update Upload
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
              uploadFiles (files) {
                  let postFiles = Array.prototype.slice.call(files);
                  if (!this.multiple) postFiles = postFiles.slice(0, 1);
  
                  if (postFiles.length === 0) return;
  
                  postFiles.forEach(file => {
                      this.upload(file);
                  });
              },
              upload (file) {
                  if (!this.beforeUpload) {
                      return this.post(file);
                  }
  
                  const before = this.beforeUpload(file);
                  if (before && before.then) {
                      before.then(processedFile => {
                          if (Object.prototype.toString.call(processedFile) === '[object File]') {
                              this.post(processedFile);
                          } else {
                              this.post(file);
                          }
                      }, () => {
                          // this.$emit('cancel', file);
                      });
                  } else if (before !== false) {
                      this.post(file);
                  } else {
                      // this.$emit('cancel', file);
                  }
              },
              post (file) {
a6ac0a76   梁灏   update Upload
225
226
227
228
229
                  // check format
                  if (this.format.length) {
                      const _file_format = file.name.split('.').pop().toLocaleLowerCase();
                      const checked = this.format.some(item => item.toLocaleLowerCase() === _file_format);
                      if (!checked) {
3c2f68f2   梁灏   update Upload
230
                          this.onFormatError(file, this.fileList);
a6ac0a76   梁灏   update Upload
231
232
233
234
235
236
237
                          return false;
                      }
                  }
  
                  // check maxSize
                  if (this.maxSize) {
                      if (file.size > this.maxSize * 1024) {
3c2f68f2   梁灏   update Upload
238
                          this.onExceededSize(file, this.fileList);
a6ac0a76   梁灏   update Upload
239
240
241
242
                          return false;
                      }
                  }
  
9671827f   梁灏   update Upload
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
                  this.handleStart(file);
                  let formData = new FormData();
                  formData.append(this.name, file);
  
                  ajax({
                      headers: this.headers,
                      withCredentials: this.withCredentials,
                      file: file,
                      data: this.data,
                      filename: this.name,
                      action: this.action,
                      onProgress: e => {
                          this.handleProgress(e, file);
                      },
                      onSuccess: res => {
                          this.handleSuccess(res, file);
                      },
                      onError: (err, response) => {
                          this.handleError(err, response, file);
                      }
                  });
              },
              handleStart (file) {
                  file.uid = Date.now() + this.tempIndex++;
                  const _file = {
                      status: 'uploading',
                      name: file.name,
                      size: file.size,
                      percentage: 0,
                      uid: file.uid,
                      showProgress: true
                  };
  
9671827f   梁灏   update Upload
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
                  this.fileList.push(_file);
              },
              getFile (file) {
                  const fileList = this.fileList;
                  let target;
                  fileList.every(item => {
                      target = file.uid === item.uid ? item : null;
                      return !target;
                  });
                  return target;
              },
              handleProgress (e, file) {
                  const _file = this.getFile(file);
                  this.onProgress(e, _file, this.fileList);
                  _file.percentage = e.percent || 0;
              },
              handleSuccess (res, file) {
                  const _file = this.getFile(file);
  
                  if (_file) {
                      _file.status = 'finished';
                      _file.response = res;
  
9671827f   梁灏   update Upload
299
                      this.onSuccess(res, _file, this.fileList);
3f91ed4d   li.fameng   Fix on-form-chang...
300
                      this.dispatch('FormItem', 'on-form-change', _file);
9671827f   梁灏   update Upload
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
  
                      setTimeout(() => {
                          _file.showProgress = false;
                      }, 1000);
                  }
              },
              handleError (err, response, file) {
                  const _file = this.getFile(file);
                  const fileList = this.fileList;
  
                  _file.status = 'fail';
  
                  fileList.splice(fileList.indexOf(_file), 1);
  
                  this.onError(err, response, file);
              },
              handleRemove(file) {
                  const fileList = this.fileList;
                  fileList.splice(fileList.indexOf(file), 1);
                  this.onRemove(file, fileList);
              },
              handlePreview(file) {
                  if (file.status === 'finished') {
                      this.onPreview(file);
                  }
              },
              clearFiles() {
                  this.fileList = [];
              }
          },
          watch: {
              defaultFileList: {
                  immediate: true,
                  handler(fileList) {
                      this.fileList = fileList.map(item => {
                          item.status = 'finished';
                          item.percentage = 100;
                          item.uid = Date.now() + this.tempIndex++;
                          return item;
                      });
                  }
              }
3affd6f3   梁灏   init upload compo...
343
          },
3affd6f3   梁灏   init upload compo...
344
      };
3f91ed4d   li.fameng   Fix on-form-chang...
345
  </script>