File Upload Preview
This recipe shows how to use pocket-vue to provide a preview for an image before it is uploaded to the server.
Example Code
html
<div v-scope="{
previewUrl: null,
handleFileChange(event) {
const file = event.target.files[0];
if (file) {
this.previewUrl = URL.createObjectURL(file);
}
},
removeFile() {
this.previewUrl = null;
// You might also need to clear the file input
document.querySelector('input[type=file]').value = '';
}
}">
<div class="file-upload">
<label>Select an image to preview:</label>
<input type="file" accept="image/*" @change="handleFileChange($event)">
<div v-if="previewUrl" class="preview">
<img :src="previewUrl" alt="Preview Image" style="max-width: 200px;">
<button type="button" @click="removeFile">Remove Image</button>
</div>
</div>
</div>How it works
- State Property: We use
previewUrlto store the temporary URL for the selected image. - Event Handling: We use
@change="handleFileChange($event)"to listen for the file input change event. - File Reader: Inside
handleFileChange, we access the file from the event target and useURL.createObjectURL(file)to create a temporary URL for the image. - Reactive Binding: We bind the
srcattribute of an<img>tag to thepreviewUrlproperty using:src="previewUrl". - Conditional Rendering: We only show the image preview and remove button if
previewUrlis not null. - Cleanup: We provide a
removeFilemethod to clear the preview and reset the file input.
