国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

ASP.NET下上傳圖片到數(shù)據(jù)庫(kù),并且讀出圖片的代碼(詳細(xì)版)

2020-01-18 00:46:30
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
首先在SQL Server中建立一個(gè)圖片存儲(chǔ)的數(shù)庫(kù)表,ImageData Column為圖象二進(jìn)制數(shù)據(jù)儲(chǔ)存字段,ImageContentType Column為圖象文件類型記錄字段,ImageDescription Column為儲(chǔ)蓄圖
象文件說(shuō)明字段,ImageSize Column為儲(chǔ)存圖象文件長(zhǎng)度字段,結(jié)構(gòu)如下:
復(fù)制代碼 代碼如下:

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程序內(nèi)容如下:
復(fù)制代碼 代碼如下:

<%@ 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>
文件說(shuō)明(添加上傳圖片說(shuō)明,如:作者、出處)
</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程序內(nèi)容如下:
復(fù)制代碼 代碼如下:

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控件對(duì)象
protected TextBox txtDescription;
protected Label txtMessage;
protected Int32 FileLength = 0; //記錄文件長(zhǎng)度變量
protected void Button_Submit(System.Object sender, System.EventArgs e) {
HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile對(duì)象,用于讀取圖象文件屬性
FileLength = UpFile.ContentLength; //記錄文件長(zhǎng)度
try {
if (FileLength == 0) { //文件長(zhǎng)度為零時(shí)
txtMessage.Text = "<b>請(qǐng)你選擇你要上傳的文件</b>";
} else {
Byte[] FileByteArray = new Byte[FileLength]; //圖象文件臨時(shí)儲(chǔ)存Byte數(shù)組
Stream StreamObject = UpFile.InputStream; //建立數(shù)據(jù)流對(duì)像
//讀取圖象文件數(shù)據(jù),F(xiàn)ileByteArray為數(shù)據(jù)儲(chǔ)存體,0為數(shù)據(jù)指針位置、FileLnegth為數(shù)據(jù)長(zhǎng)度
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; //記錄文件類型
//把其它單表數(shù)據(jù)記錄上傳
CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar,200).value =
txtDescription.Text;
//記錄文件長(zhǎng)度,讀取時(shí)使用
CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt,8).value =
UpFile.ContentLength;
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();
txtMessage.Text = "<p><b>OK!你已經(jīng)成功上傳你的圖片</b>";//提示上傳成功
}
} catch (Exception ex) {
txtMessage.Text = ex.Message.ToString();
}}}}

//----------------------------------------------------------------------
//好了,圖片已經(jīng)上傳到數(shù)據(jù)庫(kù),現(xiàn)在還要干什么呢?當(dāng)然是在數(shù)據(jù)庫(kù)中讀取及顯示在Web頁(yè)中啦,
請(qǐng)看以下程序:
ReadImage.aspx程序內(nèi)容如下:
復(fù)制代碼 代碼如下:

/-----------------------------------------------------------------------
<%@ Page Inherits="ReadImage.MainDisplay" SRC="ReadImage.cs"%>
//----------------------------------------------------------------------
//ReadImage.cs程序內(nèi)容如下:
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
//建立數(shù)據(jù)庫(kù)鏈接
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"];//設(shè)定輸出文件類型
//輸出圖象文件二進(jìn)制數(shù)制
Response.OutputStream.Write((byte[])SqlReader["ImageData"], 0,
(int)SqlReader["ImageSize"]);
Response.End();
Con.Close();
//很簡(jiǎn)單吧^_^
}
}
}

最后,我們當(dāng)然要把它在Web頁(yè)面顯示出來(lái)啦
ShowImage.hml
復(fù)制代碼 代碼如下:

<html>
<body>
這個(gè)是從數(shù)據(jù)庫(kù)讀取出來(lái)的圖象:<img src="ReadImage.aspx?ImgID=1">
<body>
</html>
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 东城区| 天门市| 大庆市| 将乐县| 陆丰市| 阳高县| 双鸭山市| 定边县| 三原县| 西昌市| 启东市| 京山县| 通山县| 阿拉善右旗| 山阳县| 贞丰县| 永州市| 德清县| 太保市| 神农架林区| 南乐县| 芷江| 福清市| 蒲城县| 应用必备| 涡阳县| 恩平市| 汉川市| 天长市| 大悟县| 嘉兴市| 临汾市| 台中县| 民勤县| 夏津县| 彰化县| 汽车| 鄱阳县| 衡南县| 青铜峡市| 张家港市|