Code

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.

File upload preview 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.

How to add it

  1. Place the Javascript SRC URL in the header section of your Webflow custom code section
  2. Add a native File Upload element to a form (Business hosting required)
  3. 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
  4. Add an embed where you want the preview to be shown and add the HTML code below to that embed
  5. 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>

                    

Browse similar code snippets

More from Corey Moen

Profile →

Related Webflow questions

All answers →

Similar code snippets

All code snippets →