⑴ 如何向資料庫中添加圖片,並顯示出來。asp.net實現。這個必須要源碼啊。
存到資料庫中:
首先用一個FileUpload控制項來瀏覽選擇圖片
<asp:FileUpload ID="fulBook" runat="server" />
創個按鈕,然後.cs中按鈕的代碼
/// <summary>
/// 更新圖書,更新命令激發後,將更換的圖片上傳
/// </summary>
protected void dvBookList_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
FileUpload fulBook = this.dvBookList.FindControl("fulBook") as FileUpload;
string FileName = fulBook.FileName;
if (FileName.Trim().Trim().Length != 0)
{
string strpath = Server.MapPath("~/images/BookCovers/" + txtISBN.Text.Trim()+ ".jpg");
//圖片上傳到images/BookCovers文件夾中並改為textIDBN中的名字
fulBook.PostedFile.SaveAs(strpath);//把圖片保存在此路徑中。也可以
//也可以 string strpath = Server.MapPath("~/images/BookCovers/" );
//fulBook.PostedFile.SaveAs(strpath+FileName);原名上傳到images/BookCovers中
然後寫sql語句(INSERT INTO 表名(Title,ISBN) VALUES (『{0}','{1}'),txtISBN.Text.Trim(),srtpath)添加到資料庫……
}
}
顯示:
添加一個datalist 在datalist中添加一個圖片img
<asp:DataList ID="dlBooks" runat="server">
<ItemTemplate>
<div>
<img style="CURSOR: hand" height="121"
alt="<%# Eval("Title") %>"
src="<%# GetUrl(Eval("ISBN").ToString()) %>" width="95" hspace="4"/>
</div>
</ItemTemplate>
</asp:DataList>
其中Eva("isbn")綁定的資料庫中圖片路徑列 Eval(''title'')綁定的是資料庫中的圖片名稱的列
cs中dlBooks.DataSource = ……;
dlBooks.DataBind();
⑵ 如何在ASP.Net 中把圖片存入資料庫
一 介紹可能有很多的時候 我們急需把圖片存入到資料庫當中 在一些應用程序中 我們可能有一些敏感的資料 由於存儲在文件系統(file system)中的東西 將很容易被某些用戶盜取 所以這些數據不能存放在文件系統中
在這篇文章中 我們將討論怎樣把圖片存入到Sql 當中
在這篇文章中我們可以學到以下幾個方面的知識
插入圖片的必要條件
使用流對象
查找准備上傳的圖片的大小和類型
.怎麼使用InputStream方法?
在我們開始上傳之前 有兩件重要的事我們需要做
(1)Form 標記的 enctype 屬性應該設置成 enctype= multipart/form data
(2) 需要一個<input type=file>表單來使用戶選擇他們要上傳的文件 同時我們需要導入 System IO名稱空間來處理流對象
把以上三點應用到aspx頁面 同時我們需要對SqlServer做以下的准備
(1)需要至少含有一個圖片類型的欄位的表
(2)如果我們還有另外一個變字元類型的欄位來存儲圖片類型 那樣會更好一些
現在 我們准備了一個Sql表(包含了一個image數據類型的欄位) 還有<input type=file>標記 當然我們還得准備Submit按鈕 以便用戶在選擇了圖片以後提交 在這個按鈕的Onclick事件里 我們需要讀取選取圖片的內容 然後把它存入到表裡 那我們先來看看這個Onclick事件
提交按鈕的Onclick事件的代碼
Dim intImageSize As Int Dim strImageType As StringDim ImageStream As Stream
Gets the Size of the ImageintImageSize = PersonImage PostedFile ContentLength
Gets the Image TypestrImageType = PersonImage PostedFile ContentType
Reads the ImageImageStream = PersonImage PostedFile InputStream
Dim ImageContent(intImageSize) As ByteDim intStatus As IntegerintStatus = ImageStream Read(ImageContent intImageSize)
Create Instance of Connection and Command ObjectDim myConnection As New SqlConnection(ConfigurationSettings AppSettings( ConnectionString ))Dim myCommand As New SqlCommand( sp_person_isp myConnection)
Mark the Command as a SPROCmyCommand CommandType = CommandType StoredProcere
Add Parameters to SPROCDim prmPersonImage As New SqlParameter( @PersonImage SqlDbType Image)prmPersonImage Value = ImageContentmyCommand Parameters Add(prmPersonImage)
Dim prmPersonImageType As New SqlParameter( @PersonImageType SqlDbType VarChar )prmPersonImageType Value = strImageTypemyCommand Parameters Add(prmPersonImageType)
TrymyConnection Open()myCommand ExecuteNonQuery()myConnection Close()Response Write( New person successfully added! )Catch SQLexc As SqlExceptionResponse Write( Insert Failed Error Details are: & SQLexc ToString())End Try
這是怎麼工作的呢?PersonImage是HTMLInputFile控制項的對象 首先需要獲得圖片的大小 可以使用下面的代碼實現
intImageSize = PersonImage PostedFile ContentLength
然後返回圖片的類型使用ContenType屬性 最後 也是最重要的事就是取得Image Stream 這可以用以下代碼實現
ImageStream = PersonImage PostedFile InputStream
我們需要一個位元組型數組來存儲image 內容 讀取整個圖片可以使用Stream對象的Read方法來實現 Read(in byte[] buffer int offset int count)方法有三個參數 【關於Read方法的詳細內容可以參看 Net FrameWorkSDK】 他們是
buffer位元組數組 此方法返回時 該緩沖區包含指定的字元數組 該數組的 offset 和 (offset + count) 之間的值由從當前源中讀取的位元組替換
offsetbuffer 中的從零開始的位元組偏移量 從此處開始存儲從當前流中讀取的數據
count要從當前流中最多讀取的位元組數
這個Read方法用以下代碼實現 intStatus = ImageStream Read(ImageContent intImageSize)現在 我們已經讀取了整個圖片的內容 下一步 我們要把這些內容存入到sql 表 我們將使用存儲過程來完成插入圖片類型和圖片內容到sql 表 如果你瀏覽了上面的代碼 你將會發現我們使用了sqldbtype image的數據類型(datatype) Ok了 完成了這些 我們也就成功的把圖片存入到SqlServer中了 下面是我們編寫的aspx頁面
二 結論
lishixin/Article/program/net/201311/15186
⑶ 我要使用ASP.NET+Access實現圖片上傳存儲到Access資料庫的image中;而image存儲的是文件夾中jpg圖片路徑
<input runat="server" id="upfile" type="file" />直接給個上傳按鈕,用Jquery寫
$(document).ready(function () {
$("#upfile").uploadify({
裡面上傳的文件夾為
'folder': '../../images',
然後還有設置回傳圖片
onComplete: function(){
document.getElementById("img").src = "../../images/" + response + ".jpg";
}