-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from eperedo/issue-5
feat(SixFour): Allow multiple files
- Loading branch information
Showing
4 changed files
with
74 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,46 @@ | ||
<template> | ||
<input type="file" v-on:change="readFile" | ||
/> | ||
v-bind:multiple="allowMultiple" /> | ||
</template> | ||
|
||
<script> | ||
function data() { | ||
return { | ||
fr: null, | ||
multipleFiles: [], | ||
}; | ||
} | ||
function destroyed() { | ||
if (this.fr) { | ||
this.fr.removeEventListener('load'); | ||
} | ||
} | ||
function getSixFourData(file) { | ||
const newFile = Object.assign(file, { sixFour: this.fr.result }); | ||
this.$emit('vue-six-four', newFile); | ||
} | ||
function readFile(e) { | ||
const file = e.target.files[0]; | ||
if (file) { | ||
this.fr = new FileReader(); | ||
this.fr.addEventListener('load', getSixFourData.bind(this, file)); | ||
this.fr.readAsDataURL(file); | ||
const files = e.target.files; | ||
const totalFiles = files.length; | ||
for (let index = 0; index < totalFiles; index += 1) { | ||
const file = files[index]; | ||
let reader = new FileReader(); | ||
reader.readAsDataURL(file); | ||
reader.addEventListener('load', () => { | ||
const result = Object.assign(file, { sixFour: reader.result }); | ||
this.multipleFiles.push(result); | ||
if (totalFiles === this.multipleFiles.length) { | ||
this.$emit('vue-six-four', this.allowMultiple ? this.multipleFiles : this.multipleFiles[0]); | ||
this.multipleFiles = []; | ||
} | ||
reader = null; | ||
}); | ||
} | ||
} | ||
export default { | ||
data, | ||
name: 'vue-six-four', | ||
destroyed, | ||
methods: { | ||
readFile, | ||
}, | ||
props: { | ||
allowMultiple: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
}, | ||
}; | ||
</script> |