ASP.NET(C#) 实现将图片以二进制保存到数据库中

我采用的是ACCESS数据库,功能主要实现:真正意义上的文件类型判断,而不是简单的判断扩展名,存储过程,将图片以二进制存储到数据库中.

注意:上传大文件时,会出现错误,原因我现在还不知道.

数据库名:mydata

表名:table_img

字段:

详细实现代码:

default.aspx 

 

<% @ Page Language = " C# "  AutoEventWireup = " true "   CodeFile = " Default.aspx.cs "  Inherits = " _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 >
      
< asp:FileUpload ID = " FileUpload1 "  runat = " server "   />< br  />
        
< br  />
        
& nbsp; < asp:Button ID = " Button1 "  runat = " server "  Text = " 上传 "  OnClick = " Button1_Click "   /></ div >
      
    
</ form >
</ body >
</ html >

default.aspx.cs

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

using  System.Data.OleDb;
using  System.IO;

public   partial   class  _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        
try
        
{
            
if (FileUpload1.HasFile)
            
{
                
if (IsAllowedExtension(this.FileUpload1))
                
{
                    
//取得上传文件的大小
                    int FileLen = FileUpload1.PostedFile.ContentLength;

                    Byte[] FileData 
= new Byte[FileLen];

                    
//创建访问客户端上传文件的对象
                    HttpPostedFile hp = FileUpload1.PostedFile;

                    
//创建数据流对象
                    Stream sr = hp.InputStream;

                    
//将图片数据放到FileData数组对象实例中,0代表数组指针的起始位置,FileLen代表指针的结束位置
                    sr.Read(FileData, 0, FileLen);

                    OleDbConnection conn 
= new OleDbConnection("Provider = Microsoft.JET.OleDB.4.0;Data Source = " + Server.MapPath("mydata.mdb"));
                    conn.Open();
                    OleDbCommand cmd 
= new OleDbCommand("insert into table_img(img) values(?)", conn);

                    
//存储过程插入到数据库中
                    OleDbParameter para = new OleDbParameter("?", OleDbType.Binary);
                    para.Value 
= FileData;
                    cmd.Parameters.Add(para);
                    cmd.ExecuteNonQuery();

                    
//弹出上传成功的提示
                    Response.Write("<script languge = ‘javascript‘ type = ‘text/javascript‘>alert(‘上传成功‘);</script>");

                    
//关闭数据库连接
                    conn.Close();

                }

                
else
                
{
                    Response.Write(
"<script languge = ‘javascript‘ type = ‘text/javascript‘>alert(‘上传文件格式不对,只允许上传jpg和gif格式的文件‘);</script>");
                }

            }

            
else
            
{
                Response.Write(
"<script language = ‘javascript‘ type = ‘text/javascript‘>alert(‘请选择上传文件‘);</script>");
            }

        }

        
catch
        
{
        }



    }


    
实现真正意义上的文件类型判断

}


你可能感兴趣的:(数据库,server,C#,asp.net,buffer,button)