2个回答
展开全部
详细实现代码:
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
{
}
}
}
读取显示,读取你会吧?
需要一个页面接收intUpload_id
int intUpload_id=Convert.ToInt16(Request.QueryString[ "intUpload_id "].ToString());
SqlConnection mycn=new SqlConnection( "server=.;uid=sa;pwd=;database=upload ");
SqlCommand mycmd=new SqlCommand( "select upload_id,upload_type,upload_image from uploadinfo_image where upload_id=@upload_id ",mycn); //一个过程
mycmd.Parameters.Add( "@upload_id ",SqlDbType.Int);
mycmd.Parameters[ "@upload_id "].Value=intUpload_id;
mycmd.Connection.Open();
SqlDataReader mydr=mycmd.ExecuteReader();
if(mydr.Read())
{
Response.ContentType=mydr[ "upload_type "].ToString(); //数据库的时候需要一个字段保存图片类型,
Response.BinaryWrite((byte [])(mydr[ "upload_image "]));
}
不管图片多大其实不适宜保存在数据库中,那样实在太大了,上面代码为复制过来的,只是添加了些注释,我也仔细看过上面的代码,应该没上面问题,改改就能用,
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
{
}
}
}
读取显示,读取你会吧?
需要一个页面接收intUpload_id
int intUpload_id=Convert.ToInt16(Request.QueryString[ "intUpload_id "].ToString());
SqlConnection mycn=new SqlConnection( "server=.;uid=sa;pwd=;database=upload ");
SqlCommand mycmd=new SqlCommand( "select upload_id,upload_type,upload_image from uploadinfo_image where upload_id=@upload_id ",mycn); //一个过程
mycmd.Parameters.Add( "@upload_id ",SqlDbType.Int);
mycmd.Parameters[ "@upload_id "].Value=intUpload_id;
mycmd.Connection.Open();
SqlDataReader mydr=mycmd.ExecuteReader();
if(mydr.Read())
{
Response.ContentType=mydr[ "upload_type "].ToString(); //数据库的时候需要一个字段保存图片类型,
Response.BinaryWrite((byte [])(mydr[ "upload_image "]));
}
不管图片多大其实不适宜保存在数据库中,那样实在太大了,上面代码为复制过来的,只是添加了些注释,我也仔细看过上面的代码,应该没上面问题,改改就能用,
展开全部
插入
1:数据库里有相应的二进制字段,
2:C#里的插入语句用SqlParameter的方式将二进制(大部分是由Stream转来的)作为参数值就可以了
读取
1:一般的查询,对应的字段用byte[]接受
2:如果是网页程序只要将byte输出到Response中(从数组中一个个读取一个一个转成int后Write),如果不行将一些HTML标签删掉,C#声明的留着,如果是WinForm就 MemoryStream memoryStream = new MemoryStream(byte[]) Bitmap bitmap = new Bitmap(memoryStream) 将bitmap 给图片控件
1:数据库里有相应的二进制字段,
2:C#里的插入语句用SqlParameter的方式将二进制(大部分是由Stream转来的)作为参数值就可以了
读取
1:一般的查询,对应的字段用byte[]接受
2:如果是网页程序只要将byte输出到Response中(从数组中一个个读取一个一个转成int后Write),如果不行将一些HTML标签删掉,C#声明的留着,如果是WinForm就 MemoryStream memoryStream = new MemoryStream(byte[]) Bitmap bitmap = new Bitmap(memoryStream) 将bitmap 给图片控件
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询