上传图片代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="addCustomer.aspx.cs" Inherits="admin_addCustomer" %>

<%@ Register src="../ascx/menu.ascx" tagname="menu" tagprefix="uc1" %>

<%@ Register src="../ascx/footer.ascx" tagname="footer" tagprefix="uc2" %>

<!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 rel="Stylesheet" type="text/css" href="../images/menu.css" />
</head>
<body id="container">
    <form id="form1" runat="server">
    <div>
    
    <div style=" width:500px; margin:60px auto 0px auto; border:solid 1px #6ba6d1; text-align:center;">
        &nbsp;客户案例添加<br />
    
        客户名称:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        演示地址:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <br />
        详细地址:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
        <br />
        <br />
        客户图像:<br />
        <asp:Image ID="Image1" runat="server" Height="136px" Width="180px" />
        <br />
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">上传</asp:LinkButton>
        <br />
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <br />
        <br />
        <asp:LinkButton ID="CustAdd" runat="server" onclick="CustAdd_Click">添加</asp:LinkButton>
        <br />
        <br />
     </div>
       
        
        
    
    </div>
    </form>
</body>
</html>

 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_addCustomer : basePage
{
    public string P_str_name;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Image1.ImageUrl = "./images/addcustmer.jpg";
        }
    }

    //上传图片
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        P_str_name = this.FileUpload1.FileName;//获取上载文件的名称
        bool P_bool_fileOK = false;
        if (FileUpload1.HasFile)
        {
            String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    P_bool_fileOK = true;
                }
            }
        }

        if (P_bool_fileOK)
        {
            this.FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/Goods/") + P_str_name);//将文件保存在相应的路径下
            this.Image1.ImageUrl = "~/Images/goods/" + P_str_name;//将图片显示在Image控件上
        }
        else
        {
           // Response.Write("<script>alert('请选择.gif,.png,.jpeg,.jpg,.bmp格式的图片文件!');</script>");
        }
    }

    //添加到数据库
    protected void CustAdd_Click(object sender, EventArgs e)
    {
        string imgurl1 = Image1.ImageUrl.ToString();
        string imgurl2 = imgurl1.Substring(1,imgurl1.Length-1);
        db.sqlstr = "insert into sms_007 values ('" + TextBox1.Text + "','" + TextBox2.Text + "','."+ imgurl2 + "','"+ TextBox4.Text +"',getdate())";
        if (db.updb(db.sqlstr))
        {
           Label1.Text = "客户添加成功";
        }

    }
}

 

 

posted @ 2014-01-10 01:29  新娃互联  阅读(788)  评论(0编辑  收藏  举报