58

I'm building a basic social network and in the registration the user uploads a display image. Basically I wanted to display the image, like a preview on the same page as the form, just after they select it and before the form is submitted.

Is this possible?

Artjom B.
  • 58,311
  • 24
  • 111
  • 196
Tom
  • 2,753
  • 3
  • 25
  • 32
  • possible duplicate of [How to upload preview image before upload through JavaScript](http://stackoverflow.com/questions/4094012/how-to-upload-preview-image-before-upload-through-javascript) – Paul D. Waite Apr 27 '11 at 14:42

5 Answers5

69

Here is the complete example for previewing image before it gets upload.

HTML :

<html>
<head>
<link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src="http://goo.gl/r57ze"></script>
<![endif]-->
</head>
<body>
<input type='file' onchange="readURL(this);" />
<img id="blah" src="#" alt="your image" />
</body>
</html>

JavaScript :

function readURL(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();
    reader.onload = function (e) {
      $('#blah')
        .attr('src', e.target.result)
        .width(150)
        .height(200);
    };
    reader.readAsDataURL(input.files[0]);
  }
}
Andrew Hubbs
  • 8,842
  • 8
  • 43
  • 70
Hardik Sondagar
  • 3,796
  • 3
  • 23
  • 42
44

I found This simpler yet powerful tutorial which uses the fileReader Object. It simply creates an img element and, using the fileReader object, assigns its source attribute as the value of the form input

function previewFile() {
  var preview = document.querySelector('img');
  var file    = document.querySelector('input[type=file]').files[0];
  var reader  = new FileReader();

  reader.onloadend = function () {
    preview.src = reader.result;
  }

  if (file) {
    reader.readAsDataURL(file);
  } else {
    preview.src = "";
  }
}
<input type="file" onchange="previewFile()"><br>
<img src="" height="200" alt="Image preview...">
Cedric Ipkiss
  • 3,682
  • 2
  • 30
  • 50
17

this can be done very easily with HTML 5, see this link http://www.html5rocks.com/en/tutorials/file/dndfiles/

iEamin
  • 273
  • 4
  • 10
5

I feel we had a related discussion earlier: How to upload preview image before upload through JavaScript

Community
  • 1
  • 1
Maddy
  • 3,316
  • 9
  • 37
  • 53
0

Image can not be shown until it serves from any server. so you need to upload the image to your server to show its preview.

Naren Sisodiya
  • 6,846
  • 2
  • 22
  • 35
  • Thanks, simple but exactly what I needed :) – Tom Apr 27 '11 at 10:30
  • 8
    This is a wrong answer... although it appear on top results... see other links in this discussion or http://stackoverflow.com/questions/14069421/show-an-image-preview-before-upload – Romain Bruckert Apr 04 '17 at 15:51