如何上传图片及预览图片

来源:互联网 发布:七月算法 视频下载 编辑:程序博客网 时间:2024/06/10 07:45

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using MACHADbLayer;
using System.Text;
using System.IO;
using System.Drawing.Imaging;
namespace MACHAWebSite.Admin.ExpertsIntroduce
{
    public partial class Edit : System.Web.UI.Page
    {
        private MACHADBEntities db
        {
            get { return this.GetDataBase(); }
        }

        public Guid? ExpertsIntrId
        {
            get
            {
                if (!string.IsNullOrEmpty(this.Request.QueryString["IntrID"]))
                    return new Guid(this.Request.QueryString["IntrID"]);
                return null;
            }
        }

        public bool IsAddMode
        {
            get { return this.ExpertsIntrId == null; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack)
                return;
            if (!this.IsAddMode)
            {
                专家介绍 intr = this.db.专家介绍.FirstOrDefault(p => p.专家标识 == this.ExpertsIntrId.Value);
                this.txt专家姓名.Text = intr.专家姓名;

                foreach (ListItem lv in this.rbnList.Items)
                {
                    if (lv.Text == intr.专家性别)
                    {
                        lv.Selected = true;
                    }
                }

                foreach (ListItem lv in this.rbnList.Items)
                {
                    if (lv.Text == intr.人员类别)
                    {
                        lv.Selected = true;
                    }
                }

                this.txt专家级别.Text = intr.专家级别.ToString();
                this.Image预览.ImageUrl = "~/photo/" + intr.专家标识 + ".jpg";
                if (intr.出生日期.HasValue)
                    this.txt出生日期.Text = intr.出生日期.Value.ToString();
                this.ftbContent.Text = intr.详细信息;
            }
        }

        protected void btn提交_Click(object sender, EventArgs e)
        {

            if (this.IsAddMode)
            {
                string upfilename = this.FileUpload专家照片.PostedFile.FileName;
                string upfiletype = upfilename.Substring(upfilename.LastIndexOf('.') + 1).ToLower();
                if (upfiletype != "jpg" && upfiletype != "gif" && upfiletype != "png" && upfiletype != "bmp")
                {
                    this.lblErrMessage.Text = "上传文件类型不正确,可上传类型为(gif,jpg,png,bmp)!";
                    return;
                }
                else
                {
                    string url = this.FileUpload专家照片.PostedFile.FileName;
                    this.Image预览.ImageUrl = url;
                }
            }

            专家介绍 intr = this.IsAddMode ? new 专家介绍 { 专家标识 = Guid.NewGuid() } : this.db.GetExportsIntr(this.ExpertsIntrId.Value);
           
            if(!string.IsNullOrEmpty(this.txt出生日期.Text))
            intr.出生日期 = Convert.ToDateTime(this.txt出生日期.Text);
            intr.详细信息 = this.ftbContent.Text;
            intr.专家级别 = Convert.ToInt32(this.txt专家级别.Text);
            intr.专家姓名 = this.txt专家姓名.Text;
            intr.专家性别 = this.rbnList.SelectedValue;
            intr.人员类别 = this.rbtnList人员类别.SelectedValue;
            intr.专家照片 = this.FileUpload专家照片.FileBytes;
            this.db.SaveExpertsIntr(intr);
            if (!this.IsAddMode)
            {
                if (System.IO.File.Exists(this.GetFilePath(this.ExpertsIntrId.Value)))
                {
                    FileInfo di = new FileInfo(this.GetFilePath(this.ExpertsIntrId.Value));
                    di.Delete();
                    this.SaveImage(this.ExpertsIntrId.Value, this.GetFilePath(this.ExpertsIntrId.Value));
                }
            }
            else
            {
                if (!System.IO.File.Exists(this.GetFilePath(intr.专家标识)))
                {
                    this.SaveImage(intr.专家标识, this.GetFilePath(intr.专家标识));
                }
            }        

            this.Response.Redirect("Default.aspx");
        }

        private string GetFilePath(Guid guid)
        {
            return Server.MapPath("~/photo/") + guid.ToString() + ".jpg";
        }

        private void SaveImage(Guid guid, string filePath)
        {
            专家介绍 expert = db.专家介绍.FirstOrDefault(p => p.专家标识 == guid);
            byte[] photo = expert.专家照片;
            try
            {
                MemoryStream ms = new MemoryStream(photo);
                System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
                image.Save(filePath, ImageFormat.Jpeg);
            }
            catch { }
        }

        protected void btn取消_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("Default.aspx");
        }

        protected void btn预览_Click(object sender, EventArgs e)
        {
            string upfilename = this.FileUpload专家照片.PostedFile.FileName;
            string upfiletype = upfilename.Substring(upfilename.LastIndexOf('.') + 1).ToLower();
            if (upfiletype != "jpg" && upfiletype != "gif")
            {
                this.lblErrMessage.Text = "上传文件类型不正确,可上传类型为(gif,jpg)!";
                return;
            }
            else
            {
                string url = this.FileUpload专家照片.PostedFile.FileName;
                this.Image预览.ImageUrl = url;
            }
        }
    }
}

 

 

 

 

//后台代码

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Edit.aspx.cs" Inherits="MACHAWebSite.Admin.ExpertsIntroduce.Edit" ValidateRequest="false"%>

<%@ Register Assembly="FreeTextBox" Namespace="FreeTextBoxControls" TagPrefix="FTB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="../../Css/ThreeStyle.css" rel="stylesheet" type="text/css" />
    <script src="../../calendar2/WdatePicker.js" type="text/javascript"></script>
    <style type="text/css">
        .style1
        {
            width: 104px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align: left;background-color:#E0E0E0; width:795px" >
    <div id="content" style="Height:28px; Width:795px; background-image: url('../../images/bule-.gif'); font-family: 宋体; font-size: 17px; padding-top:3px">
       
       &nbsp;
       
        维护专家</div>
       <table  style="line-height:25px; width: 795px;" >
            <tr>
                <td style="background-color:#f0ed95; text-align:right"class="style1">
                    专家姓名:
                </td>
                <td style="width: 450px; text-align: left">
                    <asp:TextBox ID="txt专家姓名" runat="server" MaxLength="50" Width="150px"
                        style="margin-left: 3px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="请输入"
                        ControlToValidate="txt专家姓名"></asp:RequiredFieldValidator>
                </td>
                <td rowspan="5" style="width:190px;height:130px;padding:3px;text-align:left">
                    <div style="vertical-align:middle;text-align:center;border:solid 1px #9FDB66;padding-top:9px;width:100px;height:128px">
                    <asp:Image ID="Image预览" runat="server" ImageUrl="~/images/default.jpg" Height="120px" Width="85px"/>
                    </div>
                </td>
            </tr>
            <tr>
                <td style="background-color:#f0ed95; text-align:right" class="style1">
                    专家性别:
                </td>
                <td style="width: 450px; text-align: left">
                    <asp:RadioButtonList ID="rbnList" runat="server" RepeatDirection="Horizontal">
                        <asp:ListItem Text="男" Value="男" Selected="True"></asp:ListItem>
                        <asp:ListItem Text="女" Value="女"></asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr>
             <tr>
                <td style="background-color:#f0ed95; text-align:right" class="style1">
                   人员类别:
                </td>
                <td style="width: 450px; text-align: left">
                    <asp:RadioButtonList ID="rbtnList人员类别" runat="server" RepeatDirection="Horizontal">
                        <asp:ListItem Text="领导" Value="领导"></asp:ListItem>
                        <asp:ListItem Text="专家" Value="专家"  Selected="True"></asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr>
            <tr>
                <td style="background-color:#f0ed95; text-align:right" class="style1">
                    出生日期:
                </td>
                <td style="width: 450px; text-align: left">
                    <asp:TextBox ID="txt出生日期" runat="server" contentEditable="false" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd',el:$dp.$('<%=txt出生日期.ClientID%>')})"
                        onkeydown="event.returnValue=false;" onpaste="return false" Width="150px"></asp:TextBox><img
                            onclick="WdatePicker({dateFmt:'yyyy-MM-dd',el:$dp.$('<%=txt出生日期.ClientID%>')})"
                            src="/calendar2/skin/datePicker.gif" width="16" height="22" align="absmiddle">
                </td>
            </tr>
            <tr>
                <td style="background-color:#f0ed95; text-align:right" class="style1">
                    专家级别:
                </td>
                <td style="width: 450px; text-align: left">
                    <asp:TextBox ID="txt专家级别" runat="server" MaxLength="5" Width="40px"></asp:TextBox><asp:RequiredFieldValidator
                        ID="RequiredFieldValidator2" runat="server" ErrorMessage="请输入" Display="Dynamic"
                        ControlToValidate="txt专家级别"></asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="rfv专家级别" runat="server" ControlToValidate="txt专家级别"
                        ErrorMessage="必须为数字!" ValidationExpression="[0-9]+?[0-9]*$" Display="Dynamic">专家级别必须为数字!</asp:RegularExpressionValidator>
                </td>
            </tr>
            <tr>
                <td style="background-color:#f0ed95; text-align:right" class="style1">
                    上传照片:
                </td>
                <td colspan="2" style="text-align: left;width:450px ">
                    <asp:FileUpload ID="FileUpload专家照片" runat="server"  EnableTheming="True" onkeydown="event.returnValue=false;"
                        onpaste="return false" onpropertychange="Image预览.src=this.value;" 
                        Width="226px" />
                    <asp:Button ID="btn预览" runat="server" Text="预览" OnClick="btn预览_Click" CausesValidation="false"
                        Visible="false" />
                    <asp:Label ID="lblErrMessage" runat="server" ForeColor="Red"></asp:Label>
                </td>
            </tr>
            <tr>
                <td colspan="3" style="width:100%; height: 400px">
                    <FTB:FreeTextBox ID="ftbContent" runat="server" Width="730" Height="100%">
                    </FTB:FreeTextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="请输入"
                        ControlToValidate="ftbContent"></asp:RequiredFieldValidator>
                </td>
            </tr>
        </table>
        <div style=" text-align:center">
        <asp:Button ID="btn提交" runat="server" Text="提  交" OnClick="btn提交_Click" CssClass="toolbuttonBg"/>
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         <asp:Button ID="btn取消" runat="server" Text="取  消" OnClick="btn取消_Click" CausesValidation="false" CssClass="toolbuttonBg" />
        </div>
    </div>
    </form>
</body>
</html>

 

原创粉丝点击