Preview Images picked with a file input
A file picker form field and an image tag for the preview:
<input id="file" type="file" />
<img id="preview" />
Some tasty decaff JavaScript:
var file = document.getElementById('file');
var preview = document.getElementById('preview');
file.addEventListener('change', function() {
preview.src = URL.createObjectURL(this.files[0]);
}, false);
The result: <a href="http://jsbin.com/xobosacixa" target="_blank">http://jsbin.com/xobosacixa</a>
The image hasn't gone anywhere yet. It's sitting in your browser, waiting for you to submit the form.
Neat.
Written by BigBlueHat
Related protips
2 Responses
Nice. I suppose with some fragment selection, the user can even crop and send only what's of interest to the server.
over 1 year ago
·
Yeah. Wouldn't that be nice!
Browser would have to start caring about such things, though...
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Browser
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#