Sarav's Weblog

Technical Articles for RoR Developers

Tag Archives: input type file

Preview of an image before it is uploaded

Might be a old topic. But still, it will be useful for the beginners.

Using jQuery we can achieve this, The preview action will be executed all in the browser without using Ajax to upload the image. Here is the example,

<!DOCTYPE html>
<script src=""></script>
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();

reader.onload = function (e) {


<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src=""></script>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
<input type='file' onchange="readURL(this);" />
<img id="img_prev" src="#" alt="your image" />