This article will show how to show an image preview when uploading images in ASP.NET using jQuery.

The following is my jQuery script and aspx code:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Preview Image While Upload</title>
  6. <script src="jquery-1.8.2.js" type="text/javascript"></script>
  7. <script type="text/javascript">
  8. function ShowPreview(input) {
  9. if (input.files && input.files[0]) {
  10. var ImageDir = new FileReader();
  11. ImageDir.onload = function(e) {
  12. $('#impPrev').attr('src', e.target.result);
  13. }
  14. ImageDir.readAsDataURL(input.files[0]);
  15. }
  16. }
  17. </script>
  18. </head>
  19. <body>
  20. <form id="form1" runat="server">
  21. <table cellpadding="10" cellspacing="4" width="70%" align="center" style="border: Solid 10px Green;
  22. font-weight: bold; font-size: 16pt; background-color: Skyblue; color: Blue;">
  23. <tr>
  24. <td align="center">
  25. Upload Images
  26. </td>
  27. </tr>
  28. <tr>
  29. <td>
  30. Select Your File To Upload #:
  31. <input type="file" name="ImageUpload" id="ImageUpload" onchange="ShowPreview(this)" />
  32. <asp:Button ID="btnUpload" runat="server" Text="Upload" />
  33. </td>
  34. </tr>
  35. <tr>
  36. <td>
  37. <asp:Image ID="impPrev" runat="server" Height="200px" />
  38. </td>
  39. </tr>
  40. </table>
  41. </form>
  42. </body>
  43. </html>
Now run the application and select an image to upload:

upload image
Image 1

After selecting an image you can see your image.

select your fileto upload
Image 2