2019-12-19 21:50:50 -08:00
|
|
|
<style type="text/css" scoped>
|
|
|
|
.hidden-up {
|
|
|
|
opacity: 0;
|
|
|
|
position: absolute;
|
2020-02-01 14:21:22 -08:00
|
|
|
top: -50000px;
|
2019-12-19 21:50:50 -08:00
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<template>
|
2020-02-10 09:44:43 -08:00
|
|
|
<form data-tooltip="Upload File" data-inverted>
|
2020-02-01 14:21:22 -08:00
|
|
|
<label :for="`upfile-${noteId}`" class="clickable">
|
2020-02-10 09:44:43 -08:00
|
|
|
<nm-button icon="upload" :text="uploadStatusText"/>
|
2020-02-01 14:21:22 -08:00
|
|
|
</label>
|
|
|
|
<input class="hidden-up" type="file" :id="`upfile-${noteId}`" ref="file" v-on:change="handleFileUpload()" />
|
2019-12-19 21:50:50 -08:00
|
|
|
<!-- <button v-if="file" v-on:click="uploadFileToServer()">Submit</button> -->
|
2020-02-01 14:21:22 -08:00
|
|
|
</form>
|
2019-12-19 21:50:50 -08:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import axios from 'axios'
|
|
|
|
export default {
|
|
|
|
name: 'FileUploadButton',
|
|
|
|
props: [ 'noteId' ],
|
2020-02-10 09:44:43 -08:00
|
|
|
components: {
|
|
|
|
'nm-button':require('@/components/NoteMenuButtonComponent.vue').default
|
|
|
|
},
|
2019-12-19 21:50:50 -08:00
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
file: null,
|
2020-02-10 09:44:43 -08:00
|
|
|
uploadStatusText: 'Upload',
|
2019-12-19 21:50:50 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted(){
|
|
|
|
// console.log(this.noteId)
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
uploadFileToServer() {
|
|
|
|
let formData = new FormData();
|
|
|
|
formData.append('file', this.file);
|
|
|
|
formData.append('noteId', this.noteId)
|
|
|
|
console.log('>> formData >> ', formData);
|
|
|
|
|
|
|
|
// You should have a server side REST API
|
|
|
|
axios.post('/api/attachment/upload',
|
|
|
|
formData, {
|
|
|
|
headers: { 'Content-Type': 'multipart/form-data' },
|
|
|
|
onUploadProgress: ( progressEvent ) => {
|
2020-02-10 09:44:43 -08:00
|
|
|
this.uploadStatusText = parseInt(
|
2019-12-19 21:50:50 -08:00
|
|
|
Math.round( ( progressEvent.loaded * 100 ) / progressEvent.total ) )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
).then(results => {
|
2020-02-10 09:44:43 -08:00
|
|
|
this.uploadStatusText = 'Working'
|
2019-12-19 21:50:50 -08:00
|
|
|
this.file = null
|
|
|
|
|
2020-02-01 14:21:22 -08:00
|
|
|
// console.log('File upload results')
|
|
|
|
// console.log(results.data)
|
2019-12-19 21:50:50 -08:00
|
|
|
|
|
|
|
const name = results.data.fileName
|
2020-02-01 14:21:22 -08:00
|
|
|
const location = results.data.goodFileName
|
2019-12-19 21:50:50 -08:00
|
|
|
|
2020-02-10 09:44:43 -08:00
|
|
|
this.$bus.$emit('notification', 'Processing Upload')
|
|
|
|
|
2019-12-19 21:50:50 -08:00
|
|
|
if(name && location){
|
|
|
|
|
2020-02-10 09:44:43 -08:00
|
|
|
this.uploadStatusText = 'Upload File'
|
2019-12-19 21:50:50 -08:00
|
|
|
|
2020-02-10 09:44:43 -08:00
|
|
|
const imageCode = `<img alt="image" src="/api/static/thumb_${location}">`
|
2019-12-19 21:50:50 -08:00
|
|
|
|
2020-02-01 14:21:22 -08:00
|
|
|
this.$bus.$emit('new_file_upload', {noteId: this.noteId, imageCode})
|
2020-02-11 13:11:14 -08:00
|
|
|
this.$store.dispatch('fetchAndUpdateUserTotals')
|
2020-02-01 14:21:22 -08:00
|
|
|
}
|
2019-12-19 21:50:50 -08:00
|
|
|
})
|
|
|
|
.catch(results => {
|
2020-02-10 09:44:43 -08:00
|
|
|
this.uploadStatusText = 0
|
2019-12-19 21:50:50 -08:00
|
|
|
})
|
|
|
|
},
|
|
|
|
handleFileUpload() {
|
|
|
|
//Grab file and push note id to into data
|
|
|
|
this.file = this.$refs.file.files[0]
|
|
|
|
|
|
|
|
console.log('>>>> 1st element in files array >>>> ')
|
|
|
|
console.log(this.file)
|
|
|
|
|
|
|
|
if(this.file){
|
|
|
|
this.uploadFileToServer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|