How to validate image on drop in Vue Uploader
03 Sep 20266 minutes to read
The Uploader component allows you to upload all types of images by setting image/* to the allowedExtensions property, or you can directly set it to the accept attribute of the Uploader element.
By default, this behavior works when selecting a file using the browse button. However, it is not supported on drag and drop of files. You can handle this behavior manually using the selected event by filtering the file types in the application.
In the following example, image files are validated using image/*. You are able to drag and drop the image files with the extensions png, jpg, bmp, gif, and tiff to upload them.
<template>
<div>
<ejs-uploader ref="uploadObj" id='defaultfileupload' :selected="onSelect" name="UploadFiles" :autoUpload="autoUpload"
:asyncSettings="path" :allowedExtensions="extensions"></ejs-uploader>
</div>
</template>
<script setup>
import { UploaderComponent as EjsUploader } from '@syncfusion/ej2-vue-inputs';
const path = {
saveUrl: 'https://services.syncfusion.com/vue/production/api/FileUploader/Save',
removeUrl: 'https://services.syncfusion.com/vue/production/api/FileUploader/Remove'
};
const autoUpload = false;
const extensions = 'image/*';
const onSelect = (args) => {
if (args.event.type === 'drop') {
let allImages = ['png', 'jpg', 'jpeg', 'gif', 'tiff', 'bpg'];
let files = args.filesData;
let modifiedFiles = [];
for (let file of files) {
if (allImages.indexOf(file.type) === -1) {
file.status = 'File type is not allowed';
file.statusCode = '0';
}
modifiedFiles.push(file);
}
args.isModified = true;
}
};
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-inputs/styles/material.css";
#container {
visibility: hidden;
padding-left: 5%;
width: 100%;
}
#loader {
color: #008cff;
font-family: 'Helvetica Neue', 'calibiri';
font-size: 14px;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
</style><template>
<div>
<ejs-uploader ref="uploadObj" id='defaultfileupload' :selected="onSelect" name="UploadFiles" :autoUpload="autoUpload"
:asyncSettings="path" :allowedExtensions="extensions"></ejs-uploader>
</div>
</template>
<script>
import { UploaderComponent } from '@syncfusion/ej2-vue-inputs';
export default {
name: "App",
components: {
"ejs-uploader": UploaderComponent
},
data: function () {
return {
path: {
saveUrl: 'https://services.syncfusion.com/vue/production/api/FileUploader/Save',
removeUrl: 'https://services.syncfusion.com/vue/production/api/FileUploader/Remove'
},
autoUpload: false,
extensions: 'image/*'
}
},
methods: {
onSelect: function (args) {
if (args.event.type === 'drop') {
let allImages = ['png', 'jpg', 'jpeg', 'gif', 'tiff', 'bpg'];
let files = args.filesData;
let modifiedFiles = [];
for (let file of files) {
if (allImages.indexOf(file.type) === -1) {
file.status = 'File type is not allowed';
file.statusCode = '0';
}
modifiedFiles.push(file);
}
args.isModified = true;
}
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-inputs/styles/material.css";
#container {
visibility: hidden;
padding-left: 5%;
width: 100%;
}
#loader {
color: #008cff;
font-family: 'Helvetica Neue', 'calibiri';
font-size: 14px;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
</style>You can also explore Vue File Upload feature tour page for its groundbreaking features. You can also explore our Vue File Upload example to understand how to browse the files which you want to upload to the server.