'How do I trigger a hidden Quasar q-file input from an external q-btn?

I have a q-file element for selecting an image file, and a q-img element for displaying the image.

I don't like the q-file input style, so I want to hide it and trigger the image upload from an external q-btn.

But I don't know how to trigger the q-file from an external q-btn.

const { ref } = Vue
const app = Vue.createApp({
  setup () {
  
    const image = ref(null);
    const imageUrl = ref('');
    
    const handleUpload = () => {
      if (image.value) {
        imageUrl.value = URL.createObjectURL(image.value);
      }
    }
    
    const handleUploadBtnClick = () => {
      // Somehow trigger the hidden q-file selection window
    }
    
    return {
      image, imageUrl, handleUpload
    }
  }
})

app.use(Quasar)
app.mount('#q-app')
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons" rel="stylesheet" type="text/css">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/quasar.prod.css" rel="stylesheet" type="text/css">
<div id="q-app">
  <div>
    <q-file
      style="display: none"
      v-model="image"
      @update:model-value="handleUpload()"
   ></q-file>
  </div>
  <div>
    <q-btn
      type="button"
      label="Upload Photo"
      @click="handleUploadBtnClick"
   ></q-btn>
  </div>
  <div>
    <q-img
      :src="imageUrl"
      spinner-color="white"
      style="height: 140px; max-width: 150px"
    ></q-img>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/quasar.umd.prod.js"></script>


Solution 1:[1]

You can create ref to q-file , then from q-btn call it with file.value.pickFiles()

const { ref, onMounted } = Vue
const app = Vue.createApp({
  setup () {
  
    const image = ref(null);
    const imageUrl = ref('');
    const file = ref(null)
    
    const handleUpload = () => {
      if (image.value) {
        imageUrl.value = URL.createObjectURL(image.value);
      }
    }
    
    const handleUploadBtnClick = () => {
      file.value.pickFiles()
    }
    
    return {
      image, imageUrl, handleUpload, handleUploadBtnClick, file
    }
  }
})

app.use(Quasar)
app.mount('#q-app')
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons" rel="stylesheet" type="text/css">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/quasar.prod.css" rel="stylesheet" type="text/css">
<div id="q-app">
  <div>
    <q-file
      style="display: none"
      v-model="image"
      @update:model-value="handleUpload"
      ref="file"
   ></q-file>
  </div>
  <div>
    <q-btn
      type="button"
      label="Upload Photo"
      @click="handleUploadBtnClick"
   ></q-btn>
  </div>
  <div>
    <q-img
      :src="imageUrl"
      spinner-color="white"
      style="height: 140px; max-width: 150px"
    ></q-img>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/quasar.umd.prod.js"></script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1