如何单击标签上传文件并在VUE 2中显示文件名

发布于 2025-02-11 03:08:08 字数 736 浏览 1 评论 0原文

这是我的代码,但尚未添加逻辑。 我正在尝试单击标签以上传文件,而不是使用输入标签。 apply.vue

<div class="line">
  <h6>Upload CV:</h6>
  <div class="up-cv">
    <button @click="onFileChange" type="button" id="custom-button">
      <img src="../../assets/recruit/taicv.svg" alt="" />Upload
    </button>
    <input id="real-file" type="file" style="display: none" name="image" />
    <div class="name-cv">
      <h5 id="custom-text">you have not selected the file</h5>
      <img onclick="deleteFile()" src="../../assets/recruit/delete.svg" alt="" />
    </div>
  </div>
</div>

期待您的帮助,非常感谢...

Here is my code but it has not been added logic.
I'm trying to click on the label to upload the file instead of using the input tag.
Apply.vue

<div class="line">
  <h6>Upload CV:</h6>
  <div class="up-cv">
    <button @click="onFileChange" type="button" id="custom-button">
      <img src="../../assets/recruit/taicv.svg" alt="" />Upload
    </button>
    <input id="real-file" type="file" style="display: none" name="image" />
    <div class="name-cv">
      <h5 id="custom-text">you have not selected the file</h5>
      <img onclick="deleteFile()" src="../../assets/recruit/delete.svg" alt="" />
    </div>
  </div>
</div>

Looking forward to your help, thanks a lot...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷子口的你 2025-02-18 03:08:08

我已经在Stackblitz上创建了一个演示。

链接 - https://vue-j4h4a6.stackblitz.io/

我也已将源代码连接起来

<template>
 <div id="app">
<div class="line">
  <h6>Upload CV:</h6>
  <div class="up-cv">
    <button
      @click="onFileChange"
      type="button"
      id="custom-button"
    >
      <!-- <img src="../../assets/recruit/taicv.svg" alt="" /> -->
      <span class="material-symbols-outlined"> file_upload </span>
      Upload
    </button>

    <input
      id="real-file"
      type="file"
      style="display: none"
      name="image"
      @change="fileName"
    />
    <div class="name-cv">
      <h5 v-if="uploadedFileName">{{ uploadedFileName }}</h5>
      <h5 v-else id="custom-text">you have not selected the file</h5>
      <!-- <img
        onclick="deleteFile()"
        src="../../assets/recruit/delete.svg"
        alt=""
      /> -->

      <span
        v-if="uploadedFileName"
        class="material-symbols-outlined deleteBtn"
        @click="deleteFile"
      >
        delete
      </span>
     </div>
    </div>
  </div>
 </div>
</template>

<script>
 export default {
 name: 'App',
 data() {
  return {
  uploadedFileName: null,
 };
},
methods: {
onFileChange() {
  document.getElementById('real-file').click();
},
fileName(e) {
  this.uploadedFileName = e.target.value;
},
deleteFile() {
  this.uploadedFileName = null;
},
},
};
</script>

<style>
 #custom-button {
  display: inline-block;
  border: 1px solid grey;
  padding: 10px;
  cursor: pointer;
 }
 #custom-button span {
  display: inline-block;
  vertical-align: bottom;
 }
 #app {
   font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
 }
.deleteBtn {
 cursor: pointer;
 color: red;
 }
 </style>

I have created one Demo as you want on stackblitz.

link - https://vue-j4h4a6.stackblitz.io/

also I have attached source code here

<template>
 <div id="app">
<div class="line">
  <h6>Upload CV:</h6>
  <div class="up-cv">
    <button
      @click="onFileChange"
      type="button"
      id="custom-button"
    >
      <!-- <img src="../../assets/recruit/taicv.svg" alt="" /> -->
      <span class="material-symbols-outlined"> file_upload </span>
      Upload
    </button>

    <input
      id="real-file"
      type="file"
      style="display: none"
      name="image"
      @change="fileName"
    />
    <div class="name-cv">
      <h5 v-if="uploadedFileName">{{ uploadedFileName }}</h5>
      <h5 v-else id="custom-text">you have not selected the file</h5>
      <!-- <img
        onclick="deleteFile()"
        src="../../assets/recruit/delete.svg"
        alt=""
      /> -->

      <span
        v-if="uploadedFileName"
        class="material-symbols-outlined deleteBtn"
        @click="deleteFile"
      >
        delete
      </span>
     </div>
    </div>
  </div>
 </div>
</template>

<script>
 export default {
 name: 'App',
 data() {
  return {
  uploadedFileName: null,
 };
},
methods: {
onFileChange() {
  document.getElementById('real-file').click();
},
fileName(e) {
  this.uploadedFileName = e.target.value;
},
deleteFile() {
  this.uploadedFileName = null;
},
},
};
</script>

<style>
 #custom-button {
  display: inline-block;
  border: 1px solid grey;
  padding: 10px;
  cursor: pointer;
 }
 #custom-button span {
  display: inline-block;
  vertical-align: bottom;
 }
 #app {
   font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
 }
.deleteBtn {
 cursor: pointer;
 color: red;
 }
 </style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文