Showing Preview Of The Actual Im

Showing Preview Of The Actual Image Using JavaScript Before Uploading It To The Server


Here is a simple way to provide preview functionality in your Web pages. The idea is to have some kind of preview area on the HTML page where user can see the image before submitting the request to the web server. Function ShowPreview() in the following code will show the preview in the <img> tag and stuffs data from the file field which is embedded in the <FORM> object. This method can be easily extended to provide more features. In fact this can be used to create separate window to show preview of the image.

Web Form with the JavaScript function

First Function isGraphics() will check whether the requested file is an image file or not. If it�s an image file the it will show the preview in the img tag. Img tag contains a preloaded image just to avoid the Red Cross symbol (due to the unavailability of image).

Second function setImage() will use getElementById method of document to find the �Preview� id and set the source of the image, which is to be uploaded.

<%@ Page Language="vb" Codebehind="WebForm2.aspx.vb" 
<script language=�javascript�>
function isGraphics(filename) 
  var i=filename.length;  // get file length
  i=filename.lastIndexOf('.')+1;  // find extension
  var ext=filename.substr(i);  // pull out extension
    case "gif":  // GIF
    case "jpg":  // JPG
    case "jpeg":  // JPG
    return true;
    return false;
function setImage(file) 

  var image=document.getElementById("Preview");
  if(file.value.length) if(isGraphics(file.value))
    if(image.src!=file.value) image.src=file.value;
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" cellSpacing="1" 
cellPadding="1" width="100%" border="0">
<IMG id=�Preview� alt="" 
border="1" height="80" width="100"></TD>
<INPUT id="File1" 
type="file" name="File1" runat="server"></TD>

Now as our page is ready it�s now time to call the JavaScript functions. You can call it in the same way as in traditional asp or html, but we have marked the File1 control to run at server and hence we will use .NET way of calling JavaScript functions. We need to use the Add method of the control's Attributes collection. The key parameter is used to specify an attribute name. The value parameter is used to store the function name to be called.

File1.Attributes.Add(key As String, value As String)

File1.Attributes.Add("OnChange", "setImage(this)")

Now as everything is ready build the page and run in browser. Some enhancements that you can do include:

  • Instead of fixing the width and height (which I have done in this example), you can write a JavaScript to resize the image to a fixed size after some given time frame so that user will be able to view the actual resolution.
  • You can also create a html page at run time to show the image in a separate window.

Happy Programming!

Siddharth works as a Team Leader with INGRESS Consulting from last 7 months. He has 4 1/2 yrs of work experience. Currently involved in developing a CRM for INGRESS Consulting. He was also involved in developing some of India's most successful site such as, and

Posted On : 06 February 2005

Tags : ASP.NET Web Forms JavaScript