df-image Component
df-image is an image upload field: a preview of the current image, a drag & drop zone, a button that opens the browser's file dialog, and a button that pastes an image from the clipboard. It uses the backend interface of df-file.
Basic Usage
df-image bound to a vue-forms field:
data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMDAiIGhlaWdodD0iMjAwIj48cmVjdCB3aWR0aD0iMjAwIiBoZWlnaHQ9IjIwMCIgZmlsbD0iIzE5NzZkMiIvPjx0ZXh0IHg9IjUwJSIgeT0iNTAlIiBmaWxsPSJ3aGl0ZSIgZm9udC1zaXplPSIyMCIgdGV4dC1hbmNob3I9Im1pZGRsZSIgZG9taW5hbnQtYmFzZWxpbmU9Im1pZGRsZSI+UHJvZmlsZTwvdGV4dD48L3N2Zz4=
Features
- Preview of the current image
- Drag & drop, and a click-to-browse dialog
- Pasting an image from the clipboard, with the paste button or Ctrl+V
- Upload progress bar
- Deletion of a temporary upload when the field is cleared or another upload replaces it
- Downloading the current image; the value is a URL, so no
commsmethod is needed, unlikedf-file'sgetDownloadUrl - Periodic touches that keep the uploaded image on the backend
- Labels, hints and error messages
- Backend communication through the
FileCommsinterface ofdf-file
Props
In addition to common props from InputBase, this component requires:
| Prop | Type | Default | Description |
|---|---|---|---|
| comms | FileComms | Required | Object with methods for image operations |
| touchInterval | number | undefined: the plugin's defaultTouchInterval, else 60000 | Milliseconds between keep-alive touches. See Configuration for setting this application-wide instead |
Value and Backend
The value is the string comms.upload resolves to: a URL the browser can load, used unchanged as the image's src. The same URL is passed to comms.delete and comms.touch, and is stored in the form data when using DynamicForms.
Otherwise df-file's FileComms interface, its rules for an upload that runs while the field is disabled or is superseded (value format), temporary uploads and upload progress and FileGoneError handling apply unchanged, with the URL in place of the file identifier. A FileGoneError from a touch also clears the preview. getDownloadUrl is not used.
Pasting From the Clipboard
The paste button reads the clipboard with navigator.clipboard.read() and uploads the first image it holds, e.g. one copied with a browser's "Copy image". Chromium asks for the clipboard permission on the first paste; Firefox and Safari show their own "Paste" prompt on every paste. A refused permission or a dismissed prompt uploads nothing. The button is shown where the browser offers navigator.clipboard.read() (secure contexts only) and the field is neither disabled nor read-only. It sits at the top right corner, left of the replace button when an image is shown.
Ctrl+V uploads the first image of the pasted data while focus is in the field: on the file input (reached with Tab) or on the paste button.
Non-image Files
A dropped, picked or pasted file whose type is not image/* is rejected without calling comms.upload, and so is a paste-button click when the clipboard holds no image.
Events
This component emits the common events of InputBase.
Examples
Basic Example with Direct API Communication
<template>
<df-image
v-model="imageUrl"
:comms="imageComms"
label="Profile picture"
hint="PNG or JPG, max 5MB"
/>
</template>
<script setup>
import { ref } from 'vue';
import axios from 'axios';
import { DfImage } from '@dynamicforms/vuetify-inputs';
import { FileGoneError } from '@dynamicforms/vuetify-inputs';
const imageUrl = ref(null);
// Implementation of FileComms for API communication
const imageComms = {
upload: async (file, progressCallback) => {
const formData = new FormData();
formData.append('file', file);
const response = await axios.post('/api/images', formData, {
onUploadProgress: (progressEvent) => {
if (progressCallback) {
progressCallback(progressEvent.loaded, progressEvent.total);
}
}
});
return response.data.url;
},
delete: async (imageUrl) => {
await axios.delete('/api/images', { params: { url: imageUrl } });
},
touch: async (imageUrl) => {
try {
await axios.post('/api/images/touch', { url: imageUrl });
} catch (err) {
if (err.response?.status === 404) {
throw new FileGoneError('This image is no longer available. Please upload it again.');
}
throw err;
}
}
};
</script>With DynamicForms Integration
<template>
<df-image
:control="form.fields.avatar"
:comms="imageComms"
label="Profile picture"
/>
</template>
<script setup>
import { Group, Field } from '@dynamicforms/vue-forms';
import { DfImage } from '@dynamicforms/vuetify-inputs';
const form = new Group({
avatar: new Field({ value: null })
});
// Implementation of FileComms (same as above)
const imageComms = {
// ... implementation as in previous example
};
</script>