using System; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
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.Data.SqlClient; 
using System.IO; 
using System.Drawing; 
using System.Drawing.Imaging; namespace WebApplication4 

    public partial class _Default : System.Web.UI.Page 
    { 
        string filename = "";      //文件名 
        string yanzhenggeshi = ""; //验证文件扩展名 
        string path = "";          //上传的文件路径 
        System.Drawing.Image image; //画图 
        string url = "server=.;uid=sa;password=12345;database=pubs";  
        protected void Page_Load(object sender, EventArgs e) 
        { 
            if (!IsPostBack) 
            { 
                bandlist(); 
            } 
        } 
        //上传至项目文件夹photo 
        protected void Button1_Click(object sender, EventArgs e) 
        { 
            string ss = yanzheng(); 
            if(ss!="ok") 
            { 
                MessageBox(ss); 
                return; 
            } 
            Random random = new Random(); 
            int rand = random.Next(01,99); 
            filename = DateTime.Now.ToString("yyMMddhhmmss")+rand; 
            path = Server.MapPath("") + "/photo/" + filename + yanzhenggeshi; 
            FileUpload1.PostedFile.SaveAs(path); 
            image = System.Drawing.Image.FromFile(path); 
            Bitmap bt = new Bitmap(image); 
            try 
            { 
                bt.Save(Server.MapPath("") + "/photo/" + filename + yanzhenggeshi, ImageFormat.Jpeg); 
                this.Button1.Enabled = false; 
                this.Button2.Enabled = false; 
            } 
            catch(Exception ) 
            {} 
            MessageBox("上传ok!"); 
            this.Button1.Enabled = true; 
            this.Button2.Enabled = true; 
        } 
        //上传至数据库 
        protected void Button2_Click(object sender, EventArgs e) 
        { 
            string ss = yanzheng(); 
            if (ss != "ok") 
            { 
                MessageBox(ss); 
                return; 
            } 
            System.IO.Stream filestream = FileUpload1.PostedFile.InputStream; 
            int filelength = FileUpload1.PostedFile.ContentLength; 
            byte[] file = new byte[filelength];     
            filestream.Read(file, 0, filelength);  //读取流 
            string up = upload(file); 
            if (up == "ok") 
            { 
                MessageBox("上传ok!"); 
            } 
            else 
            { 
                MessageBox(up); 
            } 
        } 
        //插入图片 
        private string upload(object file) 
        { 
            SqlConnection con = new SqlConnection(url); 
            string sql = "insert into photo(img) values(@img)"; 
            string msg = ""; 
            try 
            { 
                SqlCommand cmd = new SqlCommand(sql, con); 
                con.Open(); 
                cmd.Parameters.AddWithValue("@img",file); 
                int count = cmd.ExecuteNonQuery(); 
                if (count > 0) 
                { 
                    msg = "ok"; 
                    return msg; 
                } 
                else 
                { 
                    msg="error"; 
                    return msg; 
                } 
                con.Close(); 
            } 
            catch (Exception ex) 
            { 
                msg=ex.Message.ToString(); 
                return msg; 
            } 
        } 
        //验证文件 
        private string yanzheng() 
        { 
            string ss = ""; 
            yanzhenggeshi = Path.GetExtension(FileUpload1.PostedFile.FileName).ToUpper(); 
            if (yanzhenggeshi == "" || yanzhenggeshi==null) 
            { 
                ss ="请选择要上传的文件"; 
                return ss; 
            } 
            if (yanzhenggeshi != ".JPG" && yanzhenggeshi != ".GIF") 
            { 
                ss = "格式不正确"; 
                return ss; 
            } 
            if (FileUpload1.PostedFile.ContentLength > 1048576) //不能大于1M 
            { 
                ss = "图片不能大于1M"; 
                return ss; 
            } 
            else 
            { 
                ss = "ok"; 
            } 
            return ss; 
        } 
        //弹出对话框 
        private void MessageBox(string msg) 
        { 
            Response.Write("<script language='javascript'>alert('" + msg + "')</script>"); 
        } 
        //显示图片 
        protected void Button3_Click(object sender, EventArgs e) 
        { 
            string id = this.DropDownList1.SelectedValue.ToString(); 
            SqlConnection con = new SqlConnection(url); 
            string sql = " select img from photo where id = '"+id+"'"; 
            SqlCommand cmd = new SqlCommand(sql,con); 
            con.Open(); 
            SqlDataReader dr = cmd.ExecuteReader(); 
            if(dr.Read()) 
            { 
                Response.Clear(); 
                Response.BinaryWrite((byte[])dr["img"]); 
            } 
        } 
        //绑定图片列表框 
        private void bandlist() 
        { 
            SqlConnection con = new SqlConnection(url); 
            DataSet ds = new DataSet(); 
            try 
            { 
                SqlDataAdapter dt = new SqlDataAdapter("select id from photo", con); 
                con.Open(); 
                dt.Fill(ds); 
                this.DropDownList1.DataSource = ds; 
                this.DropDownList1.DataValueField = "id"; 
                this.DataBind(); 
            }catch(Exception ex) 
            { 
                MessageBox(ex.Message.ToString()); 
            } 
            con.Close(); 
        } 
    } 
} //SQL---数据表 
//if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[photo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) 
//drop table [dbo].[photo] 
//GO //CREATE TABLE [dbo].[photo] ( 
//    [id] [int] IDENTITY (1001, 1) NOT NULL , 
//    [img] [image] NULL  
//) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 
//GO 
 aspx文件<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication4._Default" %> <!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> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     
    </div> 
    <asp:FileUpload ID="FileUpload1" runat="server" /> 
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="上传至项目文件夹"  
        Width="136px" /> 
    <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="上传至数据库"  
        Width="125px" /> 
    <asp:DropDownList ID="DropDownList1" runat="server" Height="25px" Width="123px"> 
    </asp:DropDownList> 
    <asp:Button ID="Button3" runat="server" Height="25px" onclick="Button3_Click"  
        Text="显示图片" Width="104px" /> 
    </form> 
</body> 
</html> 
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jihuomima/archive/2008/10/17/3091953.aspx

解决方案 »

  1.   

    首先是_Default文件是个CS文件吧?如果是的话我应该把该文件放在自己项目中的哪个层中?DAL DLL 还是WEB层里?
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication4._Default" %> 
     还有就是ASPX里 Inherits="WebApplication4._Default" %> 这个应该是cs文件的路径吧?     .net区新来的菜鸟,刚工作一周 总是在问问题,分不多 希望大家多帮忙谢谢了
      

  2.   

    以上代码转自http://blog.csdn.net/jihuomima/archive/2008/10/17/3091953.aspx
      

  3.   

    web 层 Inherits="WebApplication4._Default 这个是引用的类名  包括命名空间
      

  4.   

    一般页面文件都放在web层,DAL和BLL一般都是.CS文件,DAL处理些对数据库的操作,BLL处理一些逻辑功能
      

  5.   

     public partial class _Default : System.Web.UI.Page 这个应该是aspx的cs文件吧,为什么我把代码复制到default的CS的文件里他提示我所有的控件都没有了呢?
    我在ASPX文件里的控件是在上面复制的代码,怎么能解决这个错误?
      

  6.   

    行 44:             FileUpload1.PostedFile.SaveAs(path); 
    当前文件并不包含对  FuleUpload1的定义。
        
       其他的btn控件也是如此。