Show preview with <input type=file>
Demo
Examples
<script type="importmap">
{
"imports": {
"@w0s/input-file-preview": "...",
"@w0s/html-escape": "...",
"whatwg-mimetype": "..."
}
}
</script>
<script type="module">
import inputFilePreview from '@w0s/input-file-preview';
inputFilePreview(document.querySelectorAll('.js-input-file-preview')); // `getElementById()` or `getElementsByClassName()` or `getElementsByTagName()` or `querySelector()` or `querySelectorAll()`
</script>
<input type="file" class="js-input-file-preview"
data-preview="preview"
data-max-size="1048576"
/>
<template id="preview">
<output><code>${name}</code> (<data value="${size}">${size} byte</data>) cannot be previewed.</output>
</template>
HTML attributes
type
[required]
- This attribute must include
file
.
data-preview
[required]
- ID of the
<template>
element to display the preview.
data-max-size
[optional]
- The number of bytes of maximum file size to preview (Files larger than this will not be previewed, but will not result in an error). If not specified, the default value is 10 MiB.
<template>
element
<template>
element must have one <output>
element.
- 🆗
<template> <output>Message</output> </template>
- 🆗
<ul> <template> <li> <output>Message</output> </li> </template> </ul>
- 🆖
<template> <p>Message</p> </template>
- Include the error message in the
<output>
element.
${name}
in the <output>
element is converted to the file name, and ${size}
is converted to the file size (in bytes).
- e.g.
<output><code>${name}</code> (<data value="${size}">${size} byte</data>) cannot be previewed.</output>