Multipart upload() now returns promise correctly.

This commit is contained in:
Harry Hedger 2016-03-16 01:09:33 -04:00
parent df2bab0e8a
commit 53239ff048
2 changed files with 42 additions and 33 deletions

View file

@ -169,8 +169,14 @@ export default class Core {
.map(type => this.runType.bind(this, type, method))
// Run waterfall of typeMethods
return Utils.promiseWaterfall(typeMethods)
.then(result => result)
.catch(error => console.error(error))
.then(result => {
if (result[0] !== undefined) {
this.log(result)
this.log('Upload result -> success!')
return result
}
})
.catch(error => this.log('Upload result -> failed:', error))
})
}
}

View file

@ -36,40 +36,43 @@ export default class Multipart extends Plugin {
}
upload (files, current, total) {
var formPost = new FormData()
return new Promise((resolve, reject) => {
var formPost = new FormData()
// turn file into an array so we can use bundle
if (!this.opts.bundle) {
files = [files[current]]
}
for (let i in files) {
formPost.append(this.opts.fieldName, files[i])
}
var xhr = new XMLHttpRequest()
xhr.open('POST', this.opts.endpoint, true)
xhr.addEventListener('progress', (e) => {
var percentage = (e.loaded / e.total * 100).toFixed(2)
this.core.log(percentage)
// this.setProgress(percentage, current, total)
})
xhr.addEventListener('load', () => {
var upload = {}
if (this.opts.bundle) {
upload = {files: files}
} else {
upload = {file: files[current]}
// turn file into an array so we can use bundle
if (!this.opts.bundle) {
files = [files[current]]
}
return Promise.resolve(upload)
})
xhr.addEventListener('error', () => {
return Promise.reject('fucking error!')
})
for (let i in files) {
formPost.append(this.opts.fieldName, files[i])
}
xhr.send(formPost)
var xhr = new XMLHttpRequest()
xhr.open('POST', this.opts.endpoint, true)
xhr.addEventListener('progress', (e) => {
var percentage = (e.loaded / e.total * 100).toFixed(2)
this.core.log(percentage)
// this.setProgress(percentage, current, total)
})
xhr.addEventListener('load', () => {
var upload = {}
if (this.opts.bundle) {
upload = {files: files}
} else {
upload = {file: files[current]}
}
return resolve(upload)
})
xhr.addEventListener('error', () => {
return reject('fucking error!')
})
xhr.send(formPost)
})
}
}