Skip to content

Instantly share code, notes, and snippets.

@saikksub
Last active July 24, 2022 11:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saikksub/d2b1ce574fc98e8285ed388bbd5287a1 to your computer and use it in GitHub Desktop.
Save saikksub/d2b1ce574fc98e8285ed388bbd5287a1 to your computer and use it in GitHub Desktop.
Drag and Drop using VueJs + Vuetify
<template>
<div
:style="{
height
}"
>
<div
class="d-flex align-center justify-center highlight-area"
style="height: 100%;"
@drop="onDrop"
@dragover="onDragOver"
>
<h1 class="title font-weight-regular text-center">
<v-btn outlined class="mr-1" color="primary" @click="onClickImportFile">
Click here
</v-btn>
<input
id="import-ele-input_file"
:accept="fileExtns"
type="file"
style="display: none;"
>
to select the import file(s)<br>
Or<br>
Drag-and-drop the file(s) to start importing files
</h1>
</div>
</div>
</template>
<script>
export default {
props: {
height: {
type: String,
required: false,
default: () => '50vh'
},
multiple: {
type: Boolean,
required: false,
default: () => true
},
fileExtns: {
type: Array,
required: false,
default: () => []
}
},
methods: {
onDrop(e) {
e = e || window.event
e.preventDefault()
const files = []
if (e.dataTransfer.items) {
// Use DataTransferItemList interface to access the file(s)
for (let index = 0; index < e.dataTransfer.items.length; index++) {
if (e.dataTransfer.items[index].kind === 'file') {
files.push(e.dataTransfer.items[index].getAsFile())
}
}
} else {
// Use DataTransfer interface to access the file(s)
for (let index = 0; index < e.dataTransfer.files.length; index++) {
files.push(e.dataTransfer.files[index])
}
}
this.$emit('files', files)
},
onDragOver(e) {
e.preventDefault()
},
onClickImportFile () {
const inputELe = document.getElementById('import-ele-input_file')
inputELe.setAttribute('type', 'file')
if (this.multiple) {
inputELe.setAttribute('multiple', '')
}
inputELe.addEventListener('change', e => {
e = e || window.event
this.$emit('files', e.target.files)
})
inputELe.click()
}
}
}
</script>
<style scoped>
.highlight-area {
border-style: dashed;
border-color: rgb(185, 185, 185);
border-width: 3.2px;
padding: 12px 18px;
}
</style>
@saikksub
Copy link
Author

saikksub commented May 10, 2020

Register component and use it as follows:

  <drag-and-drop
    :file-extns="['.pdf'', '.csv', '.docx', '.xlsx']"
    :multiple="false"
    height="60vh"
    @files="handleFiles"
  />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment