Skip to content

Commit e6e795b

Browse files
GastroGeekrstoenescu
authored andcommitted
Add uploadFactory to the QUploader component (quasarframework#2093)
* Initial commit * Update QUploader.vue
1 parent bf0e3d7 commit e6e795b

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/components/uploader/QUploader.vue

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ export default {
181181
type: Function,
182182
required: false
183183
},
184+
uploadFactory: Function,
184185
additionalFields: {
185186
type: Array,
186187
default: () => []
@@ -377,7 +378,7 @@ export default {
377378
378379
if (this.uploading && !done) {
379380
this.$emit('remove:abort', file, file.xhr)
380-
file.xhr.abort()
381+
file.xhr && file.xhr.abort()
381382
this.uploadedSize -= file.__uploaded
382383
}
383384
else {
@@ -390,6 +391,11 @@ export default {
390391
391392
file.__removed = true
392393
this.files = this.files.filter(obj => obj.name !== name)
394+
395+
if (!this.files.length) {
396+
this.uploading = false
397+
}
398+
393399
this.__computeTotalSize()
394400
},
395401
__pick () {
@@ -398,6 +404,35 @@ export default {
398404
}
399405
},
400406
__getUploadPromise (file) {
407+
initFile(file)
408+
409+
if (this.uploadFactory) {
410+
const updateProgress = (percentage) => {
411+
let uploaded = percentage * file.size
412+
this.uploadedSize += uploaded - file.__uploaded
413+
file.__uploaded = uploaded
414+
file.__progress = Math.min(99, parseInt(percentage * 100, 10))
415+
this.$forceUpdate()
416+
}
417+
418+
return new Promise((resolve, reject) => {
419+
this.uploadFactory(file, updateProgress)
420+
.then(file => {
421+
file.__doneUploading = true
422+
file.__progress = 100
423+
this.$emit('uploaded', file)
424+
this.$forceUpdate()
425+
resolve(file)
426+
})
427+
.catch(error => {
428+
file.__failed = true
429+
this.$emit('fail', file)
430+
this.$forceUpdate()
431+
reject(error)
432+
})
433+
})
434+
}
435+
401436
const
402437
form = new FormData(),
403438
xhr = new XMLHttpRequest()
@@ -413,7 +448,6 @@ export default {
413448
return
414449
}
415450
416-
initFile(file)
417451
file.xhr = xhr
418452
return new Promise((resolve, reject) => {
419453
xhr.upload.addEventListener('progress', e => {
@@ -507,6 +541,7 @@ export default {
507541
abort () {
508542
this.xhrs.forEach(xhr => { xhr.abort() })
509543
this.uploading = false
544+
this.$emit('abort')
510545
},
511546
reset () {
512547
this.abort()

0 commit comments

Comments
 (0)