⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 图象问题总汇.txt

📁 用纯ASP代码实现图片上传并存入数据库中
💻 TXT
📖 第 1 页 / 共 2 页
字号:

TEMP_ID=session("RSDA_ID") 
Set Rs = conntemp.Execute("SELECT photo from RSDA_table where ID='"&TEMP_ID&"'") 
Response.BinaryWrite Rs("photo") 
Session.Abandon 
Response.End 
%> 
********************************** 
这里主要就是用到了一个小技巧就是利用了一个SESSION变量来实现两次同条件查询。 
大家如我上述只需少量改动,就可以实现一个页面既有文字又有图象了! 





把存储在SQL7的image字段的文件下载到客户端的ASP源代码,继续和大家分享。    


--------------------------------------------------------------------------------

 【woozhj】 于 00-2-27 11:58:44 加贴在 Joy ASP ↑:

<%
'**************************************
'文 件 名:download.asp 
'使用方法:download.asp?fid=xxx
'说  明:把SQL7的image字段存储的文件下载到客户端
'数据库结构:[表名]tabimage {fid int not null;filename varchar(100) not null;filecontent image not null}
'            fid:文件id [PK];filename:文件名;filecontent:文件二进制内容
'**************************************
Response.Buffer=True
varfileid = Request("fid")
If varfileid="" Then
   Response.write "没有指定下载文件ID。"
   Response.End
End If

OpenDB conn
SQL = "SELECT filename,filecontent FROM tabimage WHERE fid=" & varfileid
Set rs = conn.Execute(SQL)
If Not rs.Eof Then
   varfilename = rs("filename")
   varfilesize=rs("filecontent").ActualSize
   varcontent = rs("filecontent").GetChunk(varfilesize)
   Response.ContentType = "*/*"
   Response.AddHeader "Content-Length",varfilesize
   Response.AddHeader "Content-Disposition", "attachment;filename=""" & varfilename & """"
   Response.binarywrite varcontent
End If
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
Response.End

'连接数据库通用过程
Sub OpenDB (ByRef conn)
    Set conn = Server.CreateObject("ADODB.Connection")
        conn.provider="sqloledb"
        conn.ConnectionString = "driver={SQL Server};server=xxx.xxx.xxx.xxx;uid=myusername;pwd=mypassword;database=mydatabase"
        conn.Open
End Sub
%>



把存储在SQL7数据库的image字段的二进制字符串转换成普通字符串的函数写出来了,和大家分享一下。    


--------------------------------------------------------------------------------

 【woozhj】 于 2000-2-23 12:59:21 加贴在 Joy ASP ↑:

'把image字段的二进制字符串转换成普通字符串
Function bin2str(binstr)
   Dim varlen,clow,ccc,skipflag
   '中文字符Skip标志
   skipflag=0
   ccc = ""
   varlen=LenB(binstr)
   For i=1 To varlen
       If skipflag=0 Then
          clow = MidB(binstr,i,1)
          '判断是否中文的字符
          If AscB(clow) > 127 Then
             'AscW会把二进制的中文字符高位和低位反转,所以要先把中文的高低位先反转
             ccc =ccc & Chr(AscW(MidB(binstr,i+1,1) & clow))
             skipflag=1
          Else
             ccc = ccc & Chr(AscB(clow))
          End If
       Else
          skipflag=0
       End If
   Next
   bin2str = ccc
End Function 


利用ASP获得图象的实际尺寸的示例    


--------------------------------------------------------------------------------

 【飞天蜘蛛】 于 2000-1-27 14:20:16 加贴在 Joy ASP ↑:

<!--#include virtual="/learn/test/lib_graphicdetect.asp"-->
<html><head>
<TITLE>dbtable.asp</TITLE>
</head>
<body bgcolor="#FFFFFF">
<%
   graphic="images/learnaspiconmain.gif"
   HW = ReadImg(graphic)
   Response.Write graphic & " Dimensions: " & HW(0) & "x" & HW(1) & "<br>"
   response.write "<img src=""/" & graphic & """" 
   response.write height=""" & HW(0) & """
   response.write width=""" & HW(0) & "">"
%>
</body></html>



The library that is included is:

<%
Dim HW

Function AscAt(s, n)
       AscAt = Asc(Mid(s, n, 1))
End Function

Function HexAt(s, n)
       HexAt = Hex(AscAt(s, n))
End Function


Function isJPG(fichero)
       If inStr(uCase(fichero), ".JPG") <> 0 Then
       isJPG = true
       Else
       isJPG = false
       End If
End Function


Function isPNG(fichero)
       If inStr(uCase(fichero), ".PNG") <> 0 Then
       isPNG = true
       Else
       isPNG = false
       End If
End Function


Function isGIF(fichero)
       If inStr(uCase(fichero), ".GIF") <> 0 Then
       isGIF = true
       Else
       isGIF = false
       End If
End Function


Function isBMP(fichero)
       If inStr(uCase(fichero), ".BMP") <> 0 Then
       isBMP = true
       Else
       isBMP = false
       End If
End Function


Function isWMF(fichero)
       If inStr(uCase(fichero), ".WMF") <> 0 Then
       isWMF = true
       Else
       isWMF = false
       End If
End Function


Function isWebImg(f)
       If isGIF(f) Or isJPG(f) Or isPNG(f) Or isBMP(f) Or isWMF(f) Then
       isWebImg = true
       Else
       isWebImg = true
       End If
End Function


Function ReadImg(fichero)
       If isGIF(fichero) Then
       ReadImg = ReadGIF(fichero)
       Else
       If isJPG(fichero) Then
       ReadImg = ReadJPG(fichero)
       Else
       If isPNG(fichero) Then
       ReadImg = ReadPNG(fichero)
       Else
       If isBMP(fichero) Then
       ReadImg = ReadPNG(fichero)
       Else
       If isWMF(fichero) Then
       ReadImg = ReadWMF(fichero)
       Else
       ReadImg = Array(0,0)
       End If
       End If
       End If
       End If
       End If
End Function


Function ReadJPG(fichero)
    Dim fso, ts, s, HW, nbytes
       HW = Array("","")
       Set fso = CreateObject("Scripting.FileSystemObject")
       Set ts = fso.OpenTextFile(Server.MapPath("/" & fichero), 1)
       s = Right(ts.Read(167), 4)
       HW(0) = HexToDec(HexAt(s,3) & HexAt(s,4))
       HW(1) = HexToDec(HexAt(s,1) & HexAt(s,2))
       ts.Close
    ReadJPG = HW
End Function


Function ReadPNG(fichero)
    Dim fso, ts, s, HW, nbytes
       HW = Array("","")
       Set fso = CreateObject("Scripting.FileSystemObject")
       Set ts = fso.OpenTextFile(Server.MapPath("/" & fichero), 1)
       s = Right(ts.Read(24), 8)
       HW(0) = HexToDec(HexAt(s,3) & HexAt(s,4))
       HW(1) = HexToDec(HexAt(s,7) & HexAt(s,8))
       ts.Close
    ReadPNG = HW
End Function


Function ReadGIF(fichero)
    Dim fso, ts, s, HW, nbytes
       HW = Array("","")
       Set fso = CreateObject("Scripting.FileSystemObject")
       Set ts = fso.OpenTextFile(Server.MapPath("/" & fichero), 1)
       s = Right(ts.Read(10), 4)
       HW(0) = HexToDec(HexAt(s,2) & HexAt(s,1))
       HW(1) = HexToDec(HexAt(s,4) & HexAt(s,3))
       ts.Close
    ReadGIF = HW
End Function


Function ReadWMF(fichero)
    Dim fso, ts, s, HW, nbytes
       HW = Array("","")
       Set fso = CreateObject("Scripting.FileSystemObject")
       Set ts = fso.OpenTextFile(Server.MapPath("/" & fichero), 1)
       s = Right(ts.Read(14), 4)
       HW(0) = HexToDec(HexAt(s,2) & HexAt(s,1))
       HW(1) = HexToDec(HexAt(s,4) & HexAt(s,3))
       ts.Close
    ReadWMF = HW
End Function


Function ReadBMP(fichero)
    Dim fso, ts, s, HW, nbytes
       HW = Array("","")
       Set fso = CreateObject("Scripting.FileSystemObject")
       Set ts = fso.OpenTextFile(Server.MapPath("/" & fichero), 1)
       s = Right(ts.Read(24), 8)
       HW(0) = HexToDec(HexAt(s,4) & HexAt(s,3))
       HW(1) = HexToDec(HexAt(s,8) & HexAt(s,7))
       ts.Close
    ReadBMP = HW
End Function


Function isDigit(c)
       If inStr("0123456789", c) <> 0 Then
       isDigit = true
       Else
       isDigit = false
       End If
End Function


Function isHex(c)
       If inStr("0123456789ABCDEFabcdef", c) <> 0 Then
       isHex = true
       Else
       ishex = false
       End If
End Function


Function HexToDec(cadhex)
       Dim n, i, ch, decimal
       decimal = 0
       n = Len(cadhex)
       For i=1 To n
       ch = Mid(cadhex, i, 1)
       If isHex(ch) Then
       decimal = decimal * 16
       If isDigit(c) Then
       decimal = decimal + ch
       Else
       decimal = decimal + Asc(uCase(ch)) - Asc("A")
       End If
       Else
       HexToDec = -1
       End If
       Next
       HexToDec = decimal
End Function
%>

从ACCESS数据库中读取图形    


--------------------------------------------------------------------------------

 【batman】 于 99-8-1 14:54:32 加贴在 Joy ASP ↑:

如何从ACCESS数据库中读取images
1。ACCESS和FoxPro数据库中的图形格式
当浏览器遇到一个<IMG>标志时,它会根据你设定的src属性来下载文件。
这可能是一个图形文件或则是一个ASP页面。
如果是一个返回gif二进制的ASP页面
浏览器需要知道返回的是什么格式的图形文件
因为这个原因,就需要指定content type,为image/gif,image/bmp
image/jpeg或则其他什么的。
   Response.contentType = "image/gif"
但这会导致另外一个问题,那就是我们只能够显示gif格式的图象,
或则说保存在数据库中的东西只能够是gif格式的了。
但是一些数据库是使用gif格式保存的,但是另外一些则是使用
jpeg格式保存的,甚至其他是采用OLE方式来保存图形的。
所以我们必须根据图形的格式来设置response的content type.

注意的是你也可以从一个文件中新建一个位图对象,但使用这样
的方式保存在数据库中的图形格式是浏览器不能够识别的。
当你往数据库中保存图象时,你应该知道你需要使用什么格式来保存
你可以把文件中的每一个字节保存下来,或则通过ACCESS/Foxpro的把图形保存
为一个OLE格式。
你使用什么格式保存图象决定了你在ASP中用什么格式来读出图形来。
具体来说,如果你在ACCESS/FoxPro中将图形保存为bmp,gif,jpeg(
这个必须要使用到ACCESS/FoxPro的OLE对象,即使用ACCESS的插入对象
对话框来完成),这是当你使用
image/bmp时浏览器是不能够解释的。

现在假设在数据库中保存的是你所想要的图形格式
(GIF, JPEG, BMP, TIFF, 等等等等)现在来看看要怎么把它们从
数据库中读出来。

在ACCESS中使用了两个关键的技术来保存图形
1。使用了bmp格式
2。78个字节的文件头

   <%
       response.Expires = 0
       response.Buffer  = True
       response.Clear
       response.contentType = "image/bmp"
   %>
接着你要干的就是去掉那78个字节的OLE对象的文件头。
   <%
       Const OLEHEADERSIZE = 78
       nFieldSize = rs("photo").ActualSize
       oleHeader = rs("photo").GetChunk(OLEHEADERSIZE)
       imageBytes = rs("photo").GetChunk(nFieldSize - OLEHEADERSIZE)
       Response.BinaryWrite imageBytes
   %>

现在举一个例子:
如果你要得到一个职工的信息,这段信息包括一个介绍和他的图象。
并且要同时显示文字和图形。
代码如下:(其中的theImg是一个代理页面)
theImg.asp
   <% 
       response.Expires = 0
       response.Buffer  = True
       response.Clear
       response.contentType = Session("ImageType")
       response.BinaryWrite Session("ImageBytes")
       Session("ImageType") = ""
       Session("ImageBytes") = ""
       response.End
   %>


   Function SetImageForDisplay(field, contentType)
       OLEHEADERSIZE = 78    
       contentType = LCase(contentType)
       select case contentType
           case "gif", "jpeg", "bmp"
              contentType = "image/" & contentType
              bytes = field.value 
           case "ole"
              contentType = "image/bmp"  
              nFieldSize = field.ActualSize
              oleHeader = field.GetChunk(OLEHEADERSIZE)
              bytes = field.GetChunk(nFieldSize - OLEHEADERSIZE)
       end select
       Session("imageBytes") = bytes
       Session("imageType") = contentType
   End Function
'注意的是,程序中只使用了4中格式:gif, jpeg, bmp , ole . 

   <%
      sql = "select * from Employees"
      Set oRS = Server.CreateObject("ADODB.Recordset")
      oRS.CursorLocation = 3
      oRS.Open sql, "DSN=NW"
      SetImageForDisplay oRS("photo"), "ole"
      Set oRS.ActiveConnection = Nothing
   %>

要显示图象的话,只需要在另外一个asp中,假设为getEmpInfo.asp中
   <img src="theImg.asp"</img>
但这还有一个问题,因为对每个职工的图形都使用了同一个"theImg.asp"
文件,应该再小小修改一下:
   <img src="theImg.asp?temp=<%= Request.Form("empLastName")%>"</img>

最后再说一点,如何显示多幅图象呢?
也就是说如果数据库中有多个字段都保存了图形,怎么办?
其实解决办法很简单,只要给SetImageForDisplay多加一个参数
就是用来保存图形的一个session变量。
例如:
   SetImageForDisplay oRS1("photo"), "ole", "empPhoto"
   SetImageForDisplay oRS2("logo"), "gif", "compLogo"

   <img src="theImg2.asp?varName=empPhoto&temp=<%= Request.Form("empLastName")%>">
   <img src="theImg2.asp?varName=compLogo&temp=<%= Request.Form("imgCode")%>">

使用这个方法能够完成下面的功能:
1。能够从数据库中取出图形字段。(你唯一需要知道的是数据库中的图形是什么格式
   bmp?gif?jpeg?ole?)
2.采用session变量 来保存图形的字节数和content type
  asp需要这些信息来联结到<IMG>中的属性
3。只要把theImg放到你想显示图形的地方,就能够显示图象了。

关于ASP图象问题总结的一点补充    


--------------------------------------------------------------------------------

 【xufeng】 于 99-6-10 上午 09:08:30 加贴在 Joy ASP ↑:

本人随手写了篇“关于ASP图象问题总结”发到版上,结果很多朋友都写信问我关于它问题,
我在这在补充几点:
1、用PB向数据库中插入图象后,如果想从PB的客户端看到图象,那你的图象格式必须是BMP。
JPG、GIF在PB里是看不到的。
2、用IE只支持三种图象格式:BMP、JPG、GIF
3、用NETSCPAGE支持两种格式:JPG、GIF
所以如果你想同时用PB、IE、NETSCAPE看图象是不可能的,解决方法是:
PB里用控件支持JPG、GIF。
如果IE则大家都用BMP,但他有缺点就是:BMP图象文件太大,不利于网上传输。
我在这提出个最终解决办法:
在数据库里只存放图象的路径,这样就不管它什么BMP。JPG,GIF了,
是不是有点无理取闹!!! 嘿嘿。。。

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -