Trigger click event of input file from external button in Vue Uploader component

11 Jun 20245 minutes to read

You can trigger the input type file click event from external button. In the below sample, click event of input file was triggered from Essential JavaScript 2 Button.

<template>
  <div id="uploaderContainer">
    <!-- Initialize Uploader -->
    <div id="dropArea">
      <span id="drop"> Drop image (JPG, PNG) files here or <button class='e-btn e-control'
          id="browse">Browse</button></span>
    </div>
    <ejs-uploader ref="uploadObj" id='defaultfileupload' name="UploadFiles" :asyncSettings="path"></ejs-uploader>
  </div>
</template>
<script setup>

import { UploaderComponent as EjsUploader } from '@syncfusion/ej2-vue-inputs';
import { onMounted } from 'vue';

const path = {
  saveUrl: 'https://services.syncfusion.com/vue/production/api/FileUploader/Save',
  removeUrl: 'https://services.syncfusion.com/vue/production/api/FileUploader/Remove'
};

onMounted(() => {
  document.getElementById('browse').onclick = () => {
    document.getElementsByClassName('e-file-select-wrap')[0].querySelector('button').click();
  };
});
</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 {
  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%;
}

#dropArea {
  border: 1px dashed #c3c3cc;
  text-align: center;
  padding: 20px 0 10px;
}

.e-file-select-wrap {
  display: none;
}

.e-upload.e-lib.e-keyboard {
  border-bottom: 0px;
  border-top: 0px
}
</style>
<template>
  <div id="uploaderContainer">
    <!-- Initialize Uploader -->
    <div id="dropArea">
      <span id="drop"> Drop image (JPG, PNG) files here or <button class='e-btn e-control'
          id="browse">Browse</button></span>
    </div>
    <ejs-uploader ref="uploadObj" id='defaultfileupload' name="UploadFiles" :asyncSettings="path"></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'
      }
    }
  },
  mounted: function () {
    document.getElementById('browse').onclick = () => {
      document.getElementsByClassName('e-file-select-wrap')[0].querySelector('button').click();
    };
  }
}
</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 {
  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%;
}

#dropArea {
  border: 1px dashed #c3c3cc;
  text-align: center;
  padding: 20px 0 10px;
}

.e-file-select-wrap {
  display: none;
}

.e-upload.e-lib.e-keyboard {
  border-bottom: 0px;
  border-top: 0px
}
</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.