Skip to content

Commit

Permalink
Merge pull request #6 from eperedo/issue-5
Browse files Browse the repository at this point in the history
feat(SixFour): Allow multiple files
  • Loading branch information
eperedo authored Oct 15, 2017
2 parents 6b7abca + d859238 commit 31df889
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 30 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ In this event you can get the information of the whole file.
}
```

### Multiple Files

By default the component only allows to select one file, but you can change that using the allow-multiple
prop.

```html
<vue-six-four v-on:vue-six-four="getFileInfo" v-bind:allow-multiple="true">
```

The only difference is the event now return an array instead an object.

## Build Setup

``` bash
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-six-four",
"version": "1.0.7",
"version": "1.1.0",
"description": "vue component which generate base64 from a file",
"author": "Eduardo P. Rivero",
"main": "dist/vue-six-four.min.js",
Expand Down
49 changes: 39 additions & 10 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,39 @@
<h1>Vue SixFour Component</h1>
</header>
<main>
<vue-six-four v-on:vue-six-four="getFileInfo"
/>
<div class="image-container">
<img v-bind:src="file64.sixFour" width="200"
height="200" alt="Vue Six Four" />
<div class="file-info">
<pre>Name: {{file64.name}}</pre>
<pre>Type: {{file64.type}}</pre>
<pre>Size: {{file64.size}}</pre>
<pre class="six-four">SixFour: {{file64.sixFour}}</pre>
<div class="example">
<header>
<h2>Single File</h2>
</header>
<vue-six-four v-on:vue-six-four="getFileInfo"
/>
<div class="image-container">
<img v-bind:src="file64.sixFour" width="200"
height="200" alt="Vue Six Four" />
<div class="file-info">
<pre>Name: {{file64.name}}</pre>
<pre>Type: {{file64.type}}</pre>
<pre>Size: {{file64.size}}</pre>
<pre class="six-four">SixFour: {{file64.sixFour}}</pre>
</div>
</div>
</div>
<div class="example">
<header>
<h2>Multiple File</h2>
</header>
<vue-six-four v-on:vue-six-four="getFiles"
v-bind:allow-multiple="true" />
<div class="image-container" v-for="image in images"
v-bind:key="image.name">
<img v-bind:src="image.sixFour" width="200"
height="200" alt="Vue Six Four" />
<div class="file-info">
<pre>Name: {{image.name}}</pre>
<pre>Type: {{image.type}}</pre>
<pre>Size: {{image.size}}</pre>
<pre class="six-four">SixFour: {{image.sixFour}}</pre>
</div>
</div>
</div>
</main>
Expand All @@ -27,6 +50,10 @@ function getFileInfo(fileInfo) {
this.file64 = fileInfo;
}
function getFiles(files) {
this.images = files;
}
export default {
name: 'app',
components: {
Expand All @@ -35,10 +62,12 @@ export default {
data() {
return {
file64: {},
images: [],
};
},
methods: {
getFileInfo,
getFiles,
},
};
</script>
Expand Down
42 changes: 23 additions & 19 deletions src/components/VueSixFour.vue
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>

0 comments on commit 31df889

Please sign in to comment.