SoFunction
Updated on 2025-03-01

C# to switch between image upload and browsing

This article uses a complete example to describe the method of C# to realize image upload and browsing switching, which has certain reference value for C# programming. Share it for your reference.

The specific implementation code is as follows:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/">
<html xmlns="http:///1999/xhtml">
<head runat="server">
  <title>Untitled page</title>
  <script src="js/jquery-1.7." type="text/javascript"></script>
  <script src="js/jquery-1.7." type="text/javascript"></script>
  <script type="text/javascript">
    $(document).ready(function() {
      $('.upic').change(function() {
        var filename = $(this).val().replace(/.*(\/|\\)/, ""); //file name        var fileExt = (/[.]/.exec(filename)) ? /[^.]+$/.exec(()) : ''; //Suffix name        if (fileExt != "jpeg" && fileExt != "jpg" && fileExt != "gif" && fileExt != "png") {
          alert('Please select a jpg or gif image file');
          return;
        };
        $(this).parent().parent().parent().addClass('support');
        if ($.) {
          if ($. == "6.0") {
            $(this).parent().parent().siblings(".preview").html('<img style="height:auto;width:88px;" src="' + $(this).val() + '"/>');
          } else {
            var previewDiv = $(this).parent().parent().siblings(".preview");
            ({ filter: "progid:(sizingMethod=image)" });
            previewDiv[0].("").sizingMethod = "scale";
            try {
              previewDiv[0].('').src = getPath($(this)[0]);
            }
            catch (e) {
              alert( + ": " + );
              alert("Invalid picture file!");
              return;
            }
          }
        }
        else {
          //var data=$(this)[0].(0).getAsDataURL();
          var data = ($(this)[0].files[0]);
          $(this).parent().parent().siblings(".preview").html('<img style="height:auto;width:88px;" src="' + data + '"/>');
          return;
        }
        $(this).parent().parent().parent().mouseover(function() { $(this).addClass('hover'); }).mouseout(function() { $(this).removeClass('hover'); });
      });
      $('.del').click(function() {
        $(this).parent().siblings("[name='isDel']").val("1");
        var file = $(this).parent().siblings(".upload").children().children(".upic");
        ((true).val("")).remove();
        $(this).parent().siblings(".preview").after('<div class="preview"></div>').remove();
        //$(this).parent().siblings(".preview").empty();
        $(this).parent().parent().removeClass('support').mouseout().unbind('mouseover');
      });
      if ($.) {
        $('#memberPhoto').filter('.support').mouseover(function() { $(this).addClass('hover'); }).mouseout(function() { $(this).removeClass('hover'); });
      };
      //Get the value of the upload control, obj is the upload control object      function getPath(obj) {
        if (obj) {
          if (("MSIE") >= 1) {
            ();
            ("btnSave").focus(); // Under hack for IE9, if the file control gets focus, () denies access            return ().text;
          }
          else if (("Firefox") >= 1) {
            if () {
              return (0).getAsDataURL();
            }
            return ;
          }
          return ;
        }
      }
    });
  </script>
</head>
<body>
  <form  runat="server">
  <div >
    <div  style="position: relative">
      <%if (!(imagePath))
       {%>
      <div class="preview">
        <img src="../<%=imagePath%>" style="height: auto; width: 88px;" /></div>
      <%}
       else
       {%>
      <div class="preview">
      </div>
      <%}%>
      <div class="upload">
        <a class="a-sc" href="javascript:;">
          <input  class="upic" type="file" name="upic" runat="server" />
        </a>
      </div>
      <div class="act">
        <span class="del">delete</span>
      </div>
    </div>
  </div>
  </form>
</body>
</html>

public partial class Default3 : 
{
  public string imagePath = "";
  protected void Page_Load(object sender, EventArgs e)
  {

  }
}

I hope this example will be helpful to everyone's C# programming.