Skip to content

Instantly share code, notes, and snippets.

@toxeus
Created January 7, 2018 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toxeus/655496697cbd9d68199e2e821d43e25c to your computer and use it in GitHub Desktop.
Save toxeus/655496697cbd9d68199e2e821d43e25c to your computer and use it in GitHub Desktop.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file
<template>
<div class="hello">
<h2>Testing UI</h2>
<form @submit.prevent="onSubmit" action="http://localhost:8000" method="post" enctype="multipart/form-data">
<select name="region" id="reg" v-model="region">
<option v-for="region in regions" :value="region">{{region}}</option>
</select>
<div v-for="slide in slides">
<input type="checkbox" v-on:click="slide.active = !slide.active">
{{ slide.name }}
<span v-if="slide.active">
<input type="text" :name="slide.name + '_region'" :value="slide.regions[region]">
<input type="file" @change="changeFile($event, slide.name)">
</span>
</div>
<input type="submit" value="Submit">
</form>
</div>
</template>
<script>
export default {
name: 'slides',
data () {
return {
slides: [
{
name: 'C',
active: false,
file: null,
regions: {
Ti: 'Ti',
Be: 'Be'
}
},
{
name: 'D',
active: false,
file: null,
regions: {
Ti: 'TI',
Be: 'BE'
}
}
],
regions: [ 'Ticino', 'Bern' ],
region: 'Ticino'
}
},
methods: {
onSubmit: function () {
var req = new XMLHttpRequest()
req.overrideMimeType('application/json')
req.open('POST', 'http://localhost:8000/', false)
req.send(JSON.stringify(this.slides))
console.log(req.responseText)
},
changeFile: function (e, slideName) {
let slides = this.slides
var reader = new FileReader()
reader.readAsDataURL(e.target.files[0])
reader.onload = function () {
for (let i = 0; i < slides.length; i++) {
if (slides[i].name === slideName) {
slides[i].file = reader.result.split('base64,')[1]
return
}
}
}
reader.onerror = function (error) {
alert('Error: ' + error)
}
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment