ASP.NET 上会图片的代码

后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YJPSDataAccess.AgentInfoManageDataAccess;
using System.Data;
using Common;
using System.Text;

namespace YJPS.YJPSPresentation.AgentInfoManagePresentation
{
    public partial class WebForm6 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DBHandler handler = new DBHandler();
                string userName = (string)Session["UserName"];
                DataTable dt = handler.Query("select * from YJPS_HBM_Operator where UserName ='" + userName + "'");
                XuHao.Text = dt.Rows[0]["OperatorID"].ToString();
                SuoShuBuMen.Text = dt.Rows[0]["DepartmentID"].ToString();
                Name.Text = dt.Rows[0]["Name"].ToString();
                Account.Text = dt.Rows[0]["UserName"].ToString();
                Password.Text = dt.Rows[0]["Password"].ToString();
                BianHao.Text = dt.Rows[0]["StaffID"].ToString();
                string str7 = Request.QueryString["DropDownList1"];
                string stra = dt.Rows[0]["Sex"].ToString();
                if (stra == "True")
                    sex.Text = "男";
                else
                    sex.Text = "女";
                RuZhiTime.Text = dt.Rows[0]["Worktime"].ToString();
                LiZhiTime.Text = dt.Rows[0]["Offtime"].ToString();
                Phone.Text = dt.Rows[0]["Phone"].ToString();
                Email.Text = dt.Rows[0]["Email"].ToString();
                QQ.Text = dt.Rows[0]["QQNumber"].ToString();
                Add.Text = dt.Rows[0]["FamilyAddress"].ToString();
                Introduce.Text = dt.Rows[0]["Introduce"].ToString();
                StringBuilder sb = new StringBuilder();
                sb.Append(@"~/Image/OperaterImage/");
                sb.Append(dt.Rows[0]["Photo"].ToString());
                I_Image.ImageUrl = sb.ToString();
            }

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //判断用户是否选择修改了照片,不同有操作有相应的修改方式
            if (!string.IsNullOrEmpty(TB_Hide.Text))
            {
                string _delPath = L_lable1.Text;

                //删除之前照片是否成功
                if (DeletePic(_delPath))
                {
                    string _picName = I_Image.ImageUrl.Substring(12);
                    //取得临时照片的地址
                    string _sourcePath = Server.MapPath(I_Image.ImageUrl.Substring(1));

                    //设置最终保存照片的地址
                    string _destPath = Server.MapPath(@"/Image/OperaterImage/" + _picName);

                    try
                    {
                        //从临时文件夹将照片剪切到最终文件夹
                        System.IO.File.Move(_sourcePath, _delPath);
                    }
                    catch
                    {
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", "alert('修改失败!')", true);
                        return;
                    }
                    string _id = this.Account.Text;
                    AgentInfoManage _bl = new AgentInfoManage();
                    //将更改的数据提交到数据库
                    bool _b = _bl.EditAgentInfo(Password.Text.ToString(), Phone.Text.ToString(), Email.Text, int.Parse(QQ.Text), Add.Text, Introduce.Text, XuHao.Text, I_Image.ImageUrl);
                    if (_b)
                    {
                        I_Image.ImageUrl = @"/Image/OperaterImage/" + _picName;

                        TB_Path.Text = "";

                        TB_Hide.Text = "";

                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", "alert('修改成功!')", true);

                    }
                    else
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", "alert('修改失败!')", true);
                }
            }
            else
            {

                string id = this.Account.Text;
                AgentInfoManage bl = new AgentInfoManage();
                bool b = bl.EditAgentInfo(Password.Text.ToString(), Phone.Text.ToString(), Email.Text, int.Parse(QQ.Text), Add.Text, Introduce.Text, XuHao.Text,I_Image.ImageUrl);
                if (b)
                {
                   
                    TB_Path.Text = "";

                    TB_Hide.Text = "";

                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", "alert('修改成功!')", true);

                }
                else
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", "alert('修改失败!')", true);
            }
        }

        //这是一个隐藏控件触发的事件,如果用户选择更改照片,则事件发生。
        protected void TB_Hide_Click(object sender, EventArgs e)
        {
            //用一个隐藏的Label控件保存之前照片的URL。
            L_lable1.Text = I_Image.ImageUrl.ToString();

            string picFileName = DateTime.Now.Ticks.ToString() + this.filePath.FileName;
            string path = Server.MapPath("/TempImage/");
            //将用户选择的图片保存到服务端的临时文件夹TempImage里。
            try
            {
                //对其保存的照片进行异常处理,
                filePath.PostedFile.SaveAs(path + picFileName);

            }
            catch
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", "alert('选择照片失败!')", true);

                return;


            }
            StringBuilder sb = new StringBuilder();
            sb.Append(@"~/TempImage/");
            sb.Append(picFileName);
            //将选中的照片显示出来
            I_Image.ImageUrl = sb.ToString();

            //如果TB_Hide.Text不为空,则用户选择了更改照片
            TB_Hide.Text = sb.ToString();
        }

        //根据URL来删除本地的照片,前提是用户选择更改照片,否则不触发这个函数。
        private bool DeletePic(string _imageURL)
        {
            try
            {
                string DelServerPath = Server.MapPath(_imageURL);

                System.IO.File.Delete(DelServerPath);

                return true;
            }
            catch
            {
                return false;
            }
        }




    }
}

  前台:

<%@ Page Title="" Language="C#" MasterPageFile="~/PageModel.Master" AutoEventWireup="true"
CodeBehind="AgentInfo.aspx.cs" Inherits="YJPS.YJPSPresentation.AgentInfoManagePresentation.WebForm6" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<style type="text/css">
.style1
{
352px;
}
.style2
{
127px;
}
</style>
<script type="text/javascript" language="javascript">

function test(o) {
document.getElementById("<%=TB_Path.ClientID %>").value = o.value;
document.getElementById("<%=TB_Hide.ClientID %>").click();
}

</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Label runat="server" ID="L_lable1" Visible="false"></asp:Label>
<asp:Button runat="server" ID="TB_Hide" OnClick="TB_Hide_Click" />
<div>
<table border="2" style=" 88%; height: 89px;" class="table_type">
<tr>
<td class="style2">
序号
</td>
<td class="style1">
<asp:TextBox ID="XuHao" runat="server" Width="145%" Enabled="false"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
所属部门
</td>
<td class="style1">
<asp:TextBox ID="SuoShuBuMen" runat="server" Width="145%" Enabled="false"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
姓名
</td>
<td class="style1">
<asp:TextBox runat="server" ID="Name" Enabled="false" Width="145%"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
登录账号
</td>
<td class="style1">
<asp:TextBox ID="Account" runat="server" Width="145%" Enabled="false"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
登录密码
</td>
<td class="style1">
<asp:TextBox ID="Password" runat="server" Width="145%"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
员工编号
</td>
<td class="style1">
<asp:TextBox ID="BianHao" runat="server" Width="145%" Enabled="false"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
性别
</td>
<td class="style1">
<asp:TextBox ID="sex" runat="server" Width="145%" Enabled="false"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
入职时间
</td>
<td class="style1">
<asp:TextBox ID="RuZhiTime" runat="server" Width="145%" Enabled="false"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
离职时间
</td>
<td class="style1">
<asp:TextBox ID="LiZhiTime" runat="server" Width="145%" Enabled="false"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
联系电话
</td>
<td class="style1">
<asp:TextBox ID="Phone" runat="server" Width="145%"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
电子邮件
</td>
<td class="style1">
<asp:TextBox ID="Email" runat="server" Width="145%"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
QQ号码
</td>
<td class="style1">
<asp:TextBox ID="QQ" runat="server" Width="145%"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
家庭住址
</td>
<td class="style1">
<asp:TextBox ID="Add" runat="server" Width="145%"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
经纪人个人介绍
</td>
<td class="style1">
<asp:TextBox ID="Introduce" runat="server" Width="145%" Height="200px" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
照片
</td>
<td height="25" align="left" class="style1">
<asp:FileUpload runat="server" ID="filePath" onchange="test(this)" Style="display: none" />
<input type="button" value="选择照片" onclick="document.getElementById('<%=filePath.ClientID %>').click()" />
<asp:TextBox runat="server" ID="TB_Path" onfocus="this.blur()" />
<asp:Image ID="I_Image" runat="server" Height="150px" Width="100px" />
</td>
</tr>
</table>
</div>
<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button2" runat="server" Style="margin-left: 50px" Text="提交" Width="72px"
OnClick="Button2_Click" />
&nbsp;&nbsp;
</asp:Content>

原文地址:https://www.cnblogs.com/BuBu/p/2648395.html