Skip to content

Instantly share code, notes, and snippets.

@sultaniman
Last active June 6, 2018 10:32
Show Gist options
  • Select an option

  • Save sultaniman/818fa26f59ded718e9b09d2f9dbafc6c to your computer and use it in GitHub Desktop.

Select an option

Save sultaniman/818fa26f59ded718e9b09d2f9dbafc6c to your computer and use it in GitHub Desktop.
import { MAX_FILE_SIZE } from '@/upload-config'
import { uploadDocument } from '@/api/documents'
import { LoadingBar } from 'iview'
const STATUS_INITIAL = 0
const STATUS_SAVING = 1
const STATUS_SUCCESS = 2
const STATUS_FAILED = 3
const prepareFiles = (fieldName, files) => {
let formData = new FormData()
// append the files to FormData
for (let file of files) {
if (file.size < MAX_FILE_SIZE) {
formData.append(fieldName, file, file.name)
}
}
}
export const UploadMixin = {
computed: {
initial() {
return this.currentStatus === STATUS_INITIAL
},
saving() {
return this.currentStatus === STATUS_SAVING
},
success() {
return this.currentStatus === STATUS_SUCCESS
},
failure() {
return this.currentStatus === STATUS_FAILED
}
},
methods: {
filesChange(fieldName, files) {
if (!files.length) return
LoadingBar.start()
const formData = prepareFiles(fieldName, files)
this.currentStatus = STATUS_SAVING
uploadDocument(formData)
.then(this.uploadDone)
.catch(this.uploadErr)
},
uploadDone() {
LoadingBar.finish()
this.currentStatus = STATUS_SUCCESS
},
uploadErr(error) {
LoadingBar.error()
this.uploadError = error
this.currentStatus = STATUS_FAILED
},
deleteDocument() {
// TODO: Delete
},
cleanup() {
// reset form to initial state
this.currentStatus = STATUS_INITIAL
this.uploadError = null
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment