如何制作可点击的图像,以其作为输入文件。

huangapple go评论125阅读模式
英文:

How to make clickable images that acts like input file

问题

我有两个输入文件和两个图像,现在我尝试创建一个可点击的图像,它会像输入文件一样操作,并将文件名存储在其中,使用Vue 3,但是当我点击图像时,我一直收到Uncaught TypeError: Cannot read properties of null (reading 'click')错误

以下是我的代码

<template>
  <input class="form-control" type="file" @change="handleFileUpload($event, 'image1')" ref="fileInputRefs[0]" style="display: none;">
  <img src="front.png" class="images" @click="handleImageClick(0)">

  <input class="form-control" type="file" @change="handleFileUpload($event, 'image1')" ref="fileInputRefs[1]" style="display: none;">
  <img src="back.png" class="images" @click="handleImageClick(1)">
</template>

<script setup>
  const fileInputRefs = [
    ref(null),
    ref(null)
  ]

  const handleImageClick = (index) => {
    fileInputRefs[index].value.click()
  }

  const handleFileUpload = (event, id) => {
    const fileList = event.target.files
    const reader = new FileReader()
    reader.readAsDataURL(fileList[0])
    reader.onload = () => {
      if (id === 'image1') {
        file1.value = {
          file: fileList[0],
          dataUrl: reader.result
        }
      } else if (id === 'image2') {
        file2.value = {
          file: fileList[0],
          dataUrl: reader.result
        }
      }
    }
  }
</script>

希望这能帮助你解决问题。

英文:

I have two input files and two images not I am trying to create a clickable image that will act like an input file and store the file name in it using Vue 3 but I keep on getting Uncaught TypeError: Cannot read properties of null (reading &#39;click&#39;) error when I click on the image

Here is my code

 &lt;template&gt;
 &lt;input class=&quot;form-control&quot; type=&quot;file&quot; @change=&quot;handleFileUpload($event, &#39;image1&#39;)&quot;ref=&quot;fileInputRefs[0]&quot; style=&quot;display: none;&quot;&gt;
 &lt;img src=&quot;front.png&quot; class=&quot;images&quot; @click=&quot;handleImageClick(0)&quot;&gt;

 &lt;input class=&quot;form-control&quot; type=&quot;file&quot; @change=&quot;handleFileUpload($event, &#39;image1&#39;)&quot;ref=&quot;fileInputRefs[1]&quot; style=&quot;display: none;&quot;&gt;
 &lt;img src=&quot;back.png&quot; class=&quot;images&quot; @click=&quot;handleImageClick(1)&quot;&gt;


&lt;script setup&gt;
  const fileInputRefs = [
             ref(null),
             ref(null)
    ] 

 const handleImageClick = (index) =&gt; {
   fileInputRefs[index].value.click()
 }

const handleFileUpload = (event, id) =&gt; {
const fileList = event.target.files
const reader = new FileReader()
reader.readAsDataURL(fileList[0])
 reader.onload = () =&gt; {
  if (id === &#39;image1&#39;) {
     file1.value = {
      file: fileList[0],
     dataUrl: reader.result
   }
  } else if (id === &#39;image2&#39;) {
  file2.value = {
    file: fileList[0],
    dataUrl: reader.result
    }
  }
 }
}



&lt;/script&gt;

答案1

得分: 1

试图绑定 ref (:ref):

const { ref } = Vue
const app = Vue.createApp({
  setup() {
    const fileInputRefs = ref([])
    const handleImageClick = (index) => {
      fileInputRefs.value[index].click()
    }
    const handleFileUpload = (event, id) => {
      const fileList = event.target.files
      const reader = new FileReader()
      reader.readAsDataURL(fileList[0])
      reader.onload = () => {
        if (id === 'image1') {
          fileInputRefs.value = {
            file: fileList[0],
            dataUrl: reader.result
          }
        } else if (id === 'image2') {
          fileInputRefs.value[1] = {
            file: fileList[0],
            dataUrl: reader.result
          }
        }
      }
    }
    return {
      handleImageClick, handleFileUpload, fileInputRefs
    };
  },
})
app.mount('#demo')
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
  <input class="form-control" type="file" @change="handleFileUpload($event, 'image1')" 
     :ref="el => { fileInputRefs[0] = el }" style="display: none;">
  <img src="front.png" class="images" @click="handleImageClick(0)">

  <input class="form-control" type="file" @change="handleFileUpload($event, 'image2')" 
    :ref="el => { fileInputRefs[1] = el }" style="display: none;">
  <img src="back.png" class="images" @click="handleImageClick(1)">
</div>
英文:

Try to bind ref (:ref):

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const { ref } = Vue
const app = Vue.createApp({
  setup() {
    const fileInputRefs = ref([])
    const handleImageClick = (index) =&gt; {
      fileInputRefs.value[index].click()
    }
    const handleFileUpload = (event, id) =&gt; {
      const fileList = event.target.files
      const reader = new FileReader()
      reader.readAsDataURL(fileList[0])
      reader.onload = () =&gt; {
        if (id === &#39;image1&#39;) {
          fileInputRefs.value = {
            file: fileList[0],
            dataUrl: reader.result
          }
        } else if (id === &#39;image2&#39;) {
          fileInputRefs.value[1] = {
            file: fileList[0],
            dataUrl: reader.result
          }
        }
      }
    }
    return {
      handleImageClick, handleFileUpload, fileInputRefs
    };
  },
})
app.mount(&#39;#demo&#39;)

<!-- language: lang-html -->

&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;demo&quot;&gt;
  &lt;input class=&quot;form-control&quot; type=&quot;file&quot; @change=&quot;handleFileUpload($event, &#39;image1&#39;)&quot; 
     :ref=&quot;el =&gt; { fileInputRefs[0] = el }&quot; style=&quot;display: none;&quot;&gt;
  &lt;img src=&quot;front.png&quot; class=&quot;images&quot; @click=&quot;handleImageClick(0)&quot;&gt;

  &lt;input class=&quot;form-control&quot; type=&quot;file&quot; @change=&quot;handleFileUpload($event, &#39;image2&#39;)&quot; 
    :ref=&quot;el =&gt; { fileInputRefs[1] = el }&quot; style=&quot;display: none;&quot;&gt;
  &lt;img src=&quot;back.png&quot; class=&quot;images&quot; @click=&quot;handleImageClick(1)&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月7日 01:34:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定