File upload preview
Add a file upload preview option to Webflow's native File upload input field. This adds a preview window to the file upload form in Webflow's native solution.
Add a file upload preview option to Webflow's native File upload input field. This adds a preview window to the file upload form in Webflow's native solution.
How to add it
- Place the Javascript SRC URL in the header section of your Webflow custom code section
- Add a native File Upload element to a form (Business hosting required)
- Give the file upload element a unique element ID. It will match the "name" field in the element settings by default but you can change it if you want to by updating the ID
- Add an embed where you want the preview to be shown and add the HTML code below to that embed
- Add the script code to the closing body tags within your custom code page settings
The code
Javascript
//Add to header
<script src="https://www.dukelearntoprogram.com/course1/common/js/image/SimpleImage.js">
</script>
//Add to body
<script>
let imgcanvas = document.getElementById("preview");
let fileinput = document.getElementById("upload");
fileinput.onchange = upload;
function upload(e) {
var image = new SimpleImage(fileinput);
image.drawTo(imgcanvas);
}
</script>
HTML
<canvas id="preview" class="upload-preview"></canvas>