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

首頁 > 開發 > 綜合 > 正文

如何利用數據庫儲存圖檔

2024-07-21 02:10:53
字體:
來源:轉載
供稿:網友


收集最實用的網頁特效代碼!

在 sql server 當中有一款資料類型號作 image , 除了可以儲存圖檔外它還可以儲存大型的二進位資料檔, 對這一個欄位大部分的人是聽過但是不知影按怎來用, 今日的文章就要來討論如何將圖檔存入去資料庫

準備工作

為了降低這篇文章的篇幅及複雜度, 咱決定借用 upload 元件來替我們完成檔案上傳的工作, 所要使用的是 dundas 所提供免錢的上傳元件, 請到下底的網址下載 dundas upload 元件並安裝

http://www.dundas.com/
創造資料表

在這個例咱要用到 sql 內建的 pubs 資料庫來作測試, 請打開 qa 然後執行下底的創造資料表指令, 所要建立的資料表中一個欄位是紀錄檔案的 content-type, 另一個則是儲存圖檔

use pubs
create table imgdata
(
  imgid int identity not null primary key,
  contenttype varchar(20),
  filedata image
)

html 表單部分

現在來看看 html 表單的部分, 因為是用做檔案上傳因此用 enctype="multipart/form-data" , 不過要注意的是一但使用了 form-data 後表單資料的取得也就不能再用 request.form, 因為這不是這篇文章的重點所以在這就不多做解釋, 請將下底的碼存成 insert.htm

<html>
<head>
<title>資料庫存入圖檔</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="insert.asp">
  <table border="0" align="center">
    <tr>
      <td>file :</td>
      <td><input type="file" name="file" size="40"></td>
    </tr>
    <tr>
      <td></td>
      <td><input type="submit" value=" 進行上傳 "></td>
    </tr>
  </table>
</form>
</body>
</html>

程式碼

擱來看麥 asp 的部分, 請將下底的碼存成 insert.asp

<%
response.buffer = true
connstr = "provider=sqloledb;" _
  & "data source=你的電腦名稱;" _
  & "initial catalog=pubs;" _
  & "user id=sa;" _
  & "password=你的密碼"
'建立 oupload 上傳物件
set oupload = server.createobject("dundas.upload.2")
'在使用 oupload 集合 (collection) 前, 要先呼叫 save 或 savetomemory 方法
oupload.savetomemory
set ors = server.createobject("adodb.recordset")
ors.open "imgdata", connstr, 2, 3
ors.addnew
'呼叫 oupload 物件的 contenttype, binary 屬性, 已取得我們要的資料
ors("contenttype").value = oupload.files(0).contenttype
ors("filedata").value = oupload.files(0).binary
ors.update
ors.close
set ors = nothing
%>

頂高的程式假設你只上傳一個檔案, 所以使用 oupload.files(0), 如果你一次上傳一個以上的檔案, 你可以將程式小改為

...
ors.open ...
for each ofile in oupload.files
  if instr(1,ofile.contenttype,"image") <> 0 then
    ors.addnew
    ors("contenttype").value = ofile.contenttype
    ors("imgdata").value = ofile.binary
  end if
next
ors.update
...

現在你可以利用瀏覽器開啟 insert.htm 來進行上傳圖檔到資料庫的動作, 執行完後你可以 select imgdata 資料表, 應該是出現一筆資料, 不過 filedata 欄位應該是看不懂的啦!

今日的文章就先介紹到這, 下一篇文章再來介紹如何將圖檔從資料庫中拉出來!

希望這篇文章對你有幫助!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 从江县| 长泰县| 墨竹工卡县| 北票市| 新邵县| 华亭县| 陈巴尔虎旗| 苍山县| 丹凤县| 南宫市| 武宣县| 门源| 凉城县| 霍邱县| 建阳市| 大城县| 平顶山市| 贵南县| 陇南市| 临颍县| 涞水县| 鄂尔多斯市| 双辽市| 华亭县| 巴马| 商水县| 工布江达县| 丹棱县| 麦盖提县| 抚松县| 房产| 贵德县| 宣汉县| 孝昌县| 灵石县| 登封市| 神木县| 离岛区| 株洲县| 浮梁县| 台山市|