Blame view
src/pages/Common/UploadFiles.vue
2.35 KB
8fe241323
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<template> <div> <v-text-field :label="label" @click="clickInputTag" v-model="fileName" append-icon="attach_file" ></v-text-field> <input type="file" style="display: none" :id="id" :ref="reference" accept="image/*" @change="convertToBase64()" /> </div> </template> <script > export default { props: ["label", "reference", "id", "trigger"], data() { return { fileToLoad: "", fileName: "", fileInBase64: "" }; }, methods: { clickInputTag() { |
de958fcfc
|
31 |
document.getElementById(this.id).click(); |
8fe241323
|
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
}, convertToBase64(e) { //Read File var selectedFile = document.getElementById(this.id).files; //Check File is not Empty if (selectedFile.length > 0) { // Select the very first file from list this.fileToLoad = selectedFile[0]; this.fileName = selectedFile[0].name; // FileReader function for read the file. var fileReader = new FileReader(); // Onload of file read the file content let vm = this; fileReader.onload = function(fileLoadedEvent) { vm.fileInBase64 = fileLoadedEvent.target.result; |
8fe241323
|
48 49 50 |
var str = vm.fileInBase64; const [baseUrl, requiredUrl] = str.split(/,/); |
8fe241323
|
51 52 53 |
let fileData = { fileName: this.fileName, |
de958fcfc
|
54 55 |
fileInBase64: requiredUrl, trigger: "" |
8fe241323
|
56 57 58 59 60 61 62 63 64 65 |
}; vm.$emit("fileSelected", fileData); }; // Convert data to base64 fileReader.readAsDataURL(this.fileToLoad); } }, resetInput(params) { // RESET INPUT ELEMENT,fileName and selectedfile array |
de958fcfc
|
66 |
this.fileToLoad = null; |
34a2a2609
|
67 |
this.fileName = null; |
8fe241323
|
68 |
this.fileInBase64 = null; |
34a2a2609
|
69 70 |
document.getElementById(this.id).type = "text"; document.getElementById(this.id).type = "file"; |
8fe241323
|
71 72 |
let fileData = { fileName: this.fileName, |
de958fcfc
|
73 74 |
fileInBase64: this.fileInBase64, trigger: "" |
8fe241323
|
75 |
}; |
de958fcfc
|
76 |
this.$emit("fileSelected", fileData); |
8fe241323
|
77 78 |
} }, |
de958fcfc
|
79 80 81 |
watch: { respondToTrigger() { if (this.respondToTrigger == "reset") { |
de958fcfc
|
82 83 84 85 86 87 88 89 90 91 92 93 |
this.resetInput(); } } }, computed: { respondToTrigger: { get() { return this.trigger; }, set(newVal) {} // this.respondToTrigger = this.trigger; } |
8fe241323
|
94 95 96 97 98 |
} }; </script> <style scoped > </style> |