欢迎光临
我们一直在努力

在ASP.NET中存取图片到数据库的示例-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

//开发环境:window 2000、sqlserver2000、.net framework sdk正式版
//开发语言:c#、asp.net
//简介:数据库中图片存蓄及读取
//作者:engine
/*
说明:在asp中,我们用request.totalbytes、request.binaryread()来上传图片,这个可恶的binaryread()方法非常笨,单个文件上传倒没什么大事,单如果多个图片上专可就花大气力了…!而现在asp.net中将会把解决以前asp中文件上传的种种问题,使你在asp.net中轻轻松松开发出功能强大的上传程序,下面大家看看例子啦。
*/
//注意:由于作者水平有限,错误是难免的,如发现错误请指教
//email:e_engine@21cn.com

/*
首先在sql server中建立一个图片存储的数库表,imagedata column为图象二进制数据储存字段,imagecontenttype column为图象文件类型记录字段,imagedescription column为储蓄图象文件说明字段,imagesize column为储存图象文件长度字段,结构如下:
create table [dbo].[imagestore] (
[imageid] [int] identity (1, 1) not null ,
[imagedata] [image] null ,
[imagecontenttype] [varchar] (50) collate chinese_prc_ci_as null ,
[imagedescription] [varchar] (200) collate chinese_prc_ci_as null ,
[imagesize] [int] null
) on [primary] textimage_on [primary]
*/

//uploadimage.aspx程序内容如下:
<%@ page inherits=”uploadimage.uploadimage” src=”uploadimage.cs” language=”c#”%>
<html><title>上传图片</title>
<body bgcolor=”#ffffff”>
<form enctype=”multipart/form-data” runat=”server” id=”form1″>
<table runat=”server” width=”700″ align=”left” id=”table1″ cellpadding=”0″ cellspacing=”0″ border=”0″>
<tr>
<td>上传图片(选择你要上传的图片)</td>
<td>
<input type=”file” id=”up_file” runat=”server” style=”width:320″ accept=”text/*” name=”up_file”>
</td>
</tr>
<tr>
<td>
文件说明(添加上传图片说明,如:作者、出处)
</td>
<td>
<asp:textbox runat=”server” width=”239″ id=”txtdescription” maintainstate=”false” />
</td>
</tr>
<tr>
<td>
<asp:label runat=”server” id=”txtmessage” forecolor=”red” maintainstate=”false” />
</td>
<td>
<asp:button runat=”server” width=”239″ onclick=”button_submit” text=”upload image” />
</td>
</tr>
</table>
</form>
</body>
</html>
//——————————————————————-
//uploadimage.cs程序内容如下:
using system;
using system.web;
using system.io;
using system.data;
using system.data.sqlclient;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace uploadimage
{
public class uploadimage : page {
protected htmlinputfile up_file; //htmlcontrol、webcontrols控件对象
protected textbox txtdescription;
protected label txtmessage;
protected int32 filelength = 0; //记录文件长度变量
protected void button_submit(system.object sender, system.eventargs e) {
httppostedfile upfile = up_file.postedfile; //httppostedfile对象,用于读取图象文件属性
filelength = upfile.contentlength; //记录文件长度
try {
if (filelength == 0) { //文件长度为零时
txtmessage.text = “<b>请你选择你要上传的文件</b>”;
} else {
byte[] filebytearray = new byte[filelength]; //图象文件临时储存byte数组
stream streamobject = upfile.inputstream; //建立数据流对像
//读取图象文件数据,filebytearray为数据储存体,0为数据指针位置、filelnegth为数据长度
streamobject.read(filebytearray,0,filelength);
//建立sql server链接
sqlconnection con = new sqlconnection(“data source=localhost;initial catalog=testdb;user id=sa;pwd=;”);
string sqlcmd = “insert into imagestore (imagedata, imagecontenttype, imagedescription, imagesize) values (@image, @contenttype, @imagedescription, @imagesize)”;
sqlcommand cmdobj = new sqlcommand(sqlcmd, con);
cmdobj.parameters.add(“@image”,sqldbtype.binary, filelength).value = filebytearray;
cmdobj.parameters.add(“@contenttype”, sqldbtype.varchar,50).value = upfile.contenttype; //记录文件类型
//把其它单表数据记录上传
cmdobj.parameters.add(“@imagedescription”, sqldbtype.varchar,200).value = txtdescription.text;
//记录文件长度,读取时使用
cmdobj.parameters.add(“@imagesize”, sqldbtype.bigint,8).value = upfile.contentlength;
con.open();
cmdobj.executenonquery();
con.close();
txtmessage.text = “<p><b>ok!你已经成功上传你的图片</b>”;//提示上传成功
}
} catch (exception ex) {
txtmessage.text = ex.message.tostring();
}}}}
//———————————————————————-
//好了,图片已经上传到数据库,现在还要干什么呢?当然是在数据库中读取及显示在web页中啦,请看以下程序:
//readimage.aspx程序内容如下:
/———————————————————————–
<%@ page inherits=”readimage.maindisplay” src=”readimage.cs”%>
//———————————————————————-
//readimage.cs程序内容如下:
using system;
using system.data;
using system.data.sqlclient;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace readimage {
public class maindisplay : system.web.ui.page {
public void page_load(system.object sender, system.eventargs e) {
int imgid = convert.toint32(request.querystring[“imgid”]); //imgid为图片id
//建立数据库链接
sqlconnection con = new sqlconnection(“data source=king;initial catalog=testdb;user id=sa;pwd=;”);
string sqlcmd = “select * from imagestore where imageid = @imageid”;
sqlcommand cmdobj = new sqlcommand(sqlcmd, con);
cmdobj.parameters.add(“@imageid”, sqldbtype.int).value = imgid;
con.open();
sqldatareader sqlreader = cmdobj.executereader();
sqlreader.read();
response.contenttype = (string)sqlreader[“imagecontenttype”];//设定输出文件类型
//输出图象文件二进制数制
response.outputstream.write((byte[])sqlreader[“imagedata”], 0, (int)sqlreader[“imagesize”]);
response.end();
con.close();
//很简单吧^_^
}
}
}
//——————————————————————–
//最后,我们当然要把它在web页面显示出来啦
//showimage.hml
<html>
<body>
这个是从数据库读取出来的图象:<img src=”readimage.aspx?imgid=1″>
<body>
</html>
//——————————————————————
//最后,这程序当然还很多改进之处,希望大家多想想多编编一定可以写出更多的图象上传程序
//good luck,engine

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 在ASP.NET中存取图片到数据库的示例-.NET教程,Asp.Net开发
分享到: 更多 (0)