Blame view
src/pages/Common/UploadPdf.vue
2.64 KB
de958fcfc
|
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 31 32 33 34 35 36 37 38 39 40 41 |
<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", "emptyPdf"], data() { return { fileToLoad: "", fileName: "", fileInBase64: "" }; }, methods: { clickInputTag() { document.getElementById(this.id).click(); }, 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; |
01a388dfe
|
42 |
console.log("file name - ", this.fileName); |
de958fcfc
|
43 44 45 46 47 48 49 |
// 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; // Print data in console |
34a2a2609
|
50 |
// console.log(vm.fileInBase64); |
de958fcfc
|
51 52 53 |
var str = vm.fileInBase64; const [baseUrl, requiredUrl] = str.split(/,/); |
34a2a2609
|
54 |
// console.log("required - ", requiredUrl); |
de958fcfc
|
55 56 57 58 |
let fileData = { fileName: this.fileName, fileInBase64: requiredUrl, |
34a2a2609
|
59 |
emptyPdf: "" |
de958fcfc
|
60 61 62 63 64 65 66 67 68 69 |
}; vm.$emit("pdfFileSelected", fileData); }; // Convert data to base64 fileReader.readAsDataURL(this.fileToLoad); } }, resetInput(params) { // RESET INPUT ELEMENT,fileName and selectedfile array |
de958fcfc
|
70 |
this.fileToLoad = null; |
34a2a2609
|
71 |
this.fileName = null; |
de958fcfc
|
72 |
this.fileInBase64 = null; |
34a2a2609
|
73 74 |
document.getElementById(this.id).type = "text"; document.getElementById(this.id).type = "file"; |
de958fcfc
|
75 76 77 |
let fileData = { fileName: this.fileName, fileInBase64: this.fileInBase64, |
34a2a2609
|
78 |
emptyPdf: "" |
de958fcfc
|
79 80 |
}; this.$emit("pdfFileSelected", fileData); |
34a2a2609
|
81 |
// console.log("its reset - ", fileData); |
de958fcfc
|
82 83 84 85 86 |
} }, watch: { respondToTrigger() { if (this.respondToTrigger == "reset") { |
34a2a2609
|
87 |
// console.log("Reset pdf - ", this.emptyPdf); |
de958fcfc
|
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
this.resetInput(); } } }, computed: { respondToTrigger: { get() { return this.emptyPdf; }, set(newVal) {} // this.respondToTrigger = this.trigger; } } }; </script> <style scoped > </style> |