在ASP.NET中存取圖片到數據庫的示例
2024-07-10 12:55:16
供稿:網友
//開發環境:window 2000、sqlserver2000、.net framework sdk正式版
//開發語言:c#、asp.net
//簡介:數據庫中圖片存蓄及讀取
//作者:engine
/*
說明:在asp中,我們用request.totalbytes、request.binaryread()來上傳圖片,這個可惡的binaryread()方法非常笨,單個文件上傳倒沒什么大事,單如果多個圖片上專可就花大氣力了…!而現在asp.net中將會把解決以前asp中文件上傳的種種問題,使你在asp.net中輕輕松松開發出功能強大的上傳程序,下面大家看看例子啦。
*/
//注意:由于作者水平有限,錯誤是難免的,如發現錯誤請指教
//email:e_engine[email protected]
/*
首先在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