用C#怎样把图片以二进制流的形式写入ACCESS数据库,在下谢谢各位大侠了 10
1个回答
展开全部
字段:
1.id 自增长
2.img ole对象
详细实现代码:
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 />
<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
{
}
}
#region 实现真正意义上的文件类型判断
public static bool IsAllowedExtension(FileUpload hifile)
{
System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string fileclass = "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
}
catch
{
}
r.Close();
fs.Close();
if (fileclass == "255216" || fileclass == "7173")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
{
return true;
}
else
{
return false;
}
}
#endregion
}
1.id 自增长
2.img ole对象
详细实现代码:
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 />
<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
{
}
}
#region 实现真正意义上的文件类型判断
public static bool IsAllowedExtension(FileUpload hifile)
{
System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string fileclass = "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
}
catch
{
}
r.Close();
fs.Close();
if (fileclass == "255216" || fileclass == "7173")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
{
return true;
}
else
{
return false;
}
}
#endregion
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |