0

I have problem in telegram web app on macOS (Vue js). If i use input with type file, then click and choose nothing - input don't work again.

<template>
  <div>
    <button @click="triggerFileInput">Add document</button>
    <input
      type="file"
      ref="fileInput"
      style="display: none"
      @change="handleFileChange"
    />
  </div>
</template>

<script setup>
import { ref } from 'vue';

const fileInput = ref(null);

const triggerFileInput = (event) => {
  event.preventDefault();
  fileInput.value.click();
};

const handleFileChange = (event) => {
  const files = event.target.files;
  if (files.length > 0) {
    console.log(files[0]);
  }
  event.target.value = '';
};
</script>
7
  • "then click and choose nothing" -- what do you mean? click what? How is the file selection dialog being closed? Commented Jun 2 at 20:36
  • 1) click but - file upload window opened 2) click cancel - file upload window closed 3) click but - file upload window didn't open Commented Jun 2 at 20:38
  • Also, please explain what you found when you debug your code. At what line is the code not behaving like you expect? Commented Jun 2 at 20:39
  • but other events work, only change doesn't work Commented Jun 2 at 20:40
  • handleFileChange isn't called, console is empty Commented Jun 2 at 20:42

1 Answer 1

0

I think, the file input’s change event only fires if the selected files actually change. When you cancel, the input’s value remains unchanged (often empty), so the browser doesn’t trigger the change event the next time you open it. As a result, your handler doesn’t run and the file dialog may seem unresponsive.

So, To fix that problem, you have to reset the file input's value before opening the dialog.

Even if the user cancels, the next time the file input opens, the value is fresh and change will fire is if a file is selected.

This is the update code.

<template>
  <div>
    <button @click="triggerFileInput">Add document</button>
    <input
      type="file"
      ref="fileInput"
      style="display: none"
      @change="handleFileChange"
    />
  </div>
</template>

<script setup>
import { ref } from 'vue';

const fileInput = ref(null);

const triggerFileInput = (event) => {
  event.preventDefault();
  // Reset input value before opening file dialog
  fileInput.value.value = null;
  fileInput.value.click();
};

const handleFileChange = (event) => {
  const files = event.target.files;
  if (files.length > 0) {
    console.log(files[0]);
  }
  // Clear the input value after processing (optional)
  event.target.value = null;
};
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

It didn't work(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.