29 lines
642 B
JavaScript
29 lines
642 B
JavaScript
"use strict"
|
|
|
|
export default {
|
|
name: 'upload',
|
|
props: {
|
|
accept: String,
|
|
label: String,
|
|
},
|
|
template: `
|
|
<label>
|
|
<input type="file" style="display: none" @change="upload" :accept="accept" />
|
|
<span class="btn">{{label || 'Upload File'}}</span>
|
|
</label>
|
|
`,
|
|
methods: {
|
|
async upload(evt) {
|
|
const file = evt.target.files[0]
|
|
if (!file) return;
|
|
const formData = new FormData();
|
|
formData.append('file', file, file.name);
|
|
const res = await fetch('/upload', {
|
|
method: 'post',
|
|
body: formData,
|
|
})
|
|
const j = await res.json()
|
|
this.$emit('uploaded', j)
|
|
},
|
|
}
|
|
}
|