N
Nuxt10mo ago
reinacchi

File Upload Preview

how would i put a preview image upon uploading it?
6 Replies
reinacchi
reinacchiOP10mo ago
here's my code:
Muhammad Mahmoud
Muhammad Mahmoud10mo ago
First you should use $fetch instead of useFetch as this is a user-interaction based. For Image previews you create a URL from your file value and then use that in your <template>
<script setup lang="ts">
const file = ref<FileList | null>(null);
const imagePreview = computed(() =>
file.value ? URL.createObjectURL(file.value) : false
)
</script>

<template>
<p> Uploaded Image Preview </p>
<img src="imagePreview" />
</template>
<script setup lang="ts">
const file = ref<FileList | null>(null);
const imagePreview = computed(() =>
file.value ? URL.createObjectURL(file.value) : false
)
</script>

<template>
<p> Uploaded Image Preview </p>
<img src="imagePreview" />
</template>
reinacchi
reinacchiOP10mo ago
it gives me this error
No description
Muhammad Mahmoud
Muhammad Mahmoud10mo ago
You may want to log values to see what's wrong. Maybe you need to pass file.value[0] for example
reinacchi
reinacchiOP10mo ago
i've got it worked. thank you :D

Did you find this page helpful?