0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Vue.js】inputタグで選択した画像を表示する

Posted at

こんな感じで選択した画像が表示されます。

Vue Playground で試してみてください。

スクリーンショット 2024-02-17 23.20.08.png

<template>
  <form>
    <div>
      <input type="file" @change="displayImage" />
      <img :src="image" style="max-width: 500px" />
    </div>
  </form>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const image = ref<string>('');

function displayImage(event: Event) {
  let file = event.target.files[0];
  if (new RegExp('^image/(png|jpeg)').test(file.type)) {
    let url = URL.createObjectURL(file);
    image.value = url;
  } else {
    image.value = '';
  }
}
</script>

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?