I'm trying to make a file upload functionality for a chat app. My HTML looks like: (I dont use forms)
<span class="file-attach mr3">
<input id="uploadFile" name="uploadFile" type="file" @change="attachFile($event.target.files)" multiple>
<i class="w-10 ml4 icon-attach"></i>
</span>
And my typescript file looks like:
async attachFile(files) {
if (!files.length) {
return
}
const reader = new FileReader()
reader.addEventListener('loadend', () => {
this.mediaLinkDocument = {
title: this.file.type.indexOf('image') !== -1 ? '' : this.file.name,
type: this.file.type,
size: this.file.size,
}
this.sendFile(this.file)
})
Array.from(Array(files.length).keys()).forEach((id) => {
console.log(files[id])
this.file = files[id]
if (this.file.size > MAX_ATTACHMENT_SIZE) {
alert('O tamanho máximo é de 20 MB')
this.file = undefined
return
}
reader.readAsDataURL(this.file)
files = {}
})
}
The upload works, but if the user uploads the same image, it don't make the @change callback and the file is not uploaded. Can anyone help me?