Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
<script language="javascript" type="text/javascript">
      debugger
      window.onload = function () {
          var FileUpload1 = document.getElementById("FileUpload1");
          if(FileUpload1 != null)
          {
              FileUpload1.onchange = function () {
                  if (typeof (FileReader) != "undefined") {
                      debugger;
                      var dvPreview = document.getElementById("dvPreview");
                      dvPreview.innerHTML = "";
                      var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;
                      for (var i = 0; i < FileUpload1.files.length; i++) {
                          var file = FileUpload1.files[i];
                          if (regex.test(file.name.toLowerCase())) {
                              var reader = new FileReader();
                              reader.onload = function (e) {
                                  var img = document.createElement("IMG");
                                  img.height = "100";
                                  img.width = "100";
                                  img.src = e.target.result;
                                  dvPreview.appendChild(img);
                              }
                              reader.readAsDataURL(file);
                          } else {
                              alert(file.name + " is not a valid image file.");
                              dvPreview.innerHTML = "";
                              return false;
                          }
                      }
                  } else {
                      alert("This browser does not support HTML5 FileReader.");
                  }
              }
          };
      }
</script>




XML
<table>
            <tr>
                <td><asp:Label ID="lblcode" runat="server" Text="Employee"></asp:Label></td>
                <td><asp:TextBox ID="txtCode" runat="server" Width="152px"></asp:TextBox><asp:ImageButton ID="ImageButton1" OnClick="ImageButton1_Click" runat="server" ImageAlign="AbsBottom" ImageUrl="~/Images/Select.jpg" Height="21px" Width="32px" /><asp:TextBox ID="txtName" runat="server" Width="135px"></asp:TextBox></td>
                <td></td>
                <td class="auto-style2"></td>
            </tr>
            <tr>
                <td><asp:Label ID="lblDescription" runat="server" Text="Description"></asp:Label></td>
                <td colspan="4"><asp:TextBox ID="txtDescription" runat="server" Width="334px"></asp:TextBox></td>
            </tr>
            <tr>
                <td class="auto-style1"><asp:Label ID="lblpath" runat="server" Text="Path"></asp:Label></td>
                <td colspan="4" class="auto-style1">&nbsp;<input id="FileUpload1" type="file" multiple="multiple" /></td>

            </tr>
            <tr>   <td>    <%-- <hr style="width: 150px" />--%>
        <b>Live Preview</b>
        <br />
        <br />
        <div id="dvPreview">
        </div>
                </td>
                 <td style="text-align:right">
           <asp:Button ID="btnsave" runat="server" CssClass="button" Height="24px"  OnClick="btnsave_Click" Text="Save" ValidationGroup="vchk" Width="65px" />
                       <asp:Button ID="btncancle" runat="server" CssClass="button" Height="24px"
                        Text="Cancel" Width="65px" OnClick="btncancle_Click" />
                </td>

            </tr>
        </table>
Posted
Comments
Praveen Raghuvanshi 7-Jul-15 7:03am    
What is the error reported in the debug window of browser?
Member 10928697 7-Jul-15 7:08am    
This browser does not support HTML5 FileReader this error occured sir.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900