Reputation: 8479
I am currently building multiple image uploader with preview and dropzone in vue.js.
I have two questions for it:
div
. How do I fix it?You can find my working code in here: https://jsfiddle.net/bravemaster619/prxkjt9z/
<div id="app">
<h2>Images:</h2>
<div class="row m-2">
<div v-for="(image, index) in images" class="image-input image-input-active d-flex">
<div class="image-preview">
<img class="img-responsive h-100" :src="image">
<button class="btn btn-xs remove-file" @click="removeFile(index)">
<i class="fa fa-trash " ></i>
</button>
</div>
</div>
<div class="image-input image-input-tbd d-flex" v-if="this.files.length < this.option.maxFileCount">
<div class="image-preview dropzone d-flex justify-content-center align-items-center col-lg-3 col-md-4" @drop="loaddropfile" @click="openinput">
<i class="fa fa-plus text-success"></i>
</div>
<input type="file" class="d-none" id="vue-file-upload-input" @change="addImage">
</div>
</div>
<div class="row justify-content-center m-2">
<button class="btn btn-primary" @click="upload">Upload</button>
</div>
</div>
new Vue({
el: "#app",
data() {
return {
option: {
maxFileCount: 3
},
files:[],
images: [],
}
},
methods: {
loaddropfile: function(e) {
e.preventDefault()
e.stopPropagation()
alert('ok')
console.log(e)
},
openinput: function() {
document.getElementById("vue-file-upload-input").click();
},
addImage: function(e) {
const tmpFiles = e.target.files
if (tmpFiles.length === 0) {
return false;
}
const file = tmpFiles[0]
this.files.push(file)
const self = this
const reader = new FileReader()
reader.onload = function(e) {
self.images.push(e.target.result)
}
reader.readAsDataURL(file)
},
removeFile: function(index) {
this.files.splice(index, 1)
this.images.splice(index, 1)
document.getElementById("vue-file-upload-input").value = null
},
upload: function() {
alert('Check console to see uploads')
console.log(this.files)
}
},
})
Upvotes: 0
Views: 485
Reputation: 6978
On mounted just add this code and it works.
mounted(){
dropContainer.ondragover = dropContainer.ondragenter = function(evt) {
evt.preventDefault();
};
dropContainer.ondrop = function(evt) {
// pretty simple -- but not for IE :(
this.loaddropfile(evt);
evt.preventDefault();
};
}
and add the id to a div tag.
<div id="dropContainer" class="image-preview dropzone d-flex justify-content-center align-items-center" @click="openinput">
Working fiddle - https://jsfiddle.net/29z18a5g/
Upvotes: 1