请教高手,能否不用组件实现图片上传,保存于某一目录中,保存成文件,非数据库中。此想法到底有无可能实现,急用,谢谢!

flyfox 2000-08-07 01:27:00
请教高手,能否不用组件实现图片上传,保存于某一目录中,保存成文件,非数据库中。此想法到底有无可能实现,急用,谢谢!
...全文
779 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
defore 2001-08-22
  • 打赏
  • 举报
回复
upload.htm


<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
td { font-size: 9pt}
a { color: #000000; text-decoration: none}
a:hover { text-decoration: underline}
.tx { height: 16px; width: 30px; border-color: black black #000000; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; font-size: 9pt; background-color: #eeeeee; color: #0000FF}
.bt { font-size: 9pt; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; height: 16px; width: 80px; background-color: #eeeeee; cursor: hand}
.tx1 { height: 20px; width: 30px; font-size: 9pt; border: 1px solid; border-color: black black #000000; color: #0000FF}
-->
</style>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" method="post" action="upfile.asp" enctype="multipart/form-data" >
<table border="1" cellspacing="0" cellpadding="0" bordercolorlight="#000000" bordercolordark="#CCCCCC" width="91" height="23">
<tr>
<td align="left" valign="middle" height="18" width="18"> </td>
<td bgcolor="#CCCCCC" align="left" valign="middle" height="18" width="67"> 文件上传</td>
</tr>
</table>
<br>
<input type="hidden" name="act" value="upload">
<br>
<table width="71%" border="1" cellspacing="0" cellpadding="5" align="center" bordercolordark="#CCCCCC" bordercolorlight="#000000">
<tr bgcolor="#CCCCCC">
<td height="22" align="left" valign="middle" bgcolor="#CCCCCC"> 化境编程界文件上传</td>
</tr>
<tr align="left" valign="middle" bgcolor="#eeeeee">
<td bgcolor="#eeeeee" height="92">
<script language="javascript">
function setid()
{
str='<br>';
if(!window.form1.upcount.value)
window.form1.upcount.value=1;
for(i=1;i<=window.form1.upcount.value;i++)
str+='文件'+i+':<input type="file" name="file'+i+'" style="width:400" class="tx1"><br><br>';
window.upid.innerHTML=str+'<br>';
}
</script>
<li> 需要上传的个数
<input type="text" name="upcount" class="tx" value="1">
<input type="button" name="Button" class="bt" onclick="setid();" value="· 设定 ·">
</li>
<br>
<br>
<li>上传到:
<input type="text" name="filepath" class="tx" style="width:350" value="">
</li>
</td>
</tr>
<tr align="center" valign="middle">
<td align="left" id="upid" height="122"> 文件1:
<input type="file" name="file1" style="width:400" class="tx1" value="">
</td>
</tr>
<tr align="center" valign="middle" bgcolor="#eeeeee">
<td bgcolor="#eeeeee" height="24">
<input type="submit" name="Submit" value="· 提交 ·" class="bt">
<input type="reset" name="Submit2" value="· 重执 ·" class="bt">
</td>
</tr>
</table>
</form>
</body>
</html>
<script language="javascript">

setid();
</script>


upfile.asp

<%OPTION EXPLICIT%>
<!--#include FILE="upload_5xsoft.inc"-->
<html>
<head>
<title>文件上传</title>
</head>
<body>
<br>化境文件上传!<hr size=1 noshadow width=300 align=left><br><br>
<%
dim upload,file,formName,formPath,iCount
set upload=new upload_5xSoft ''建立上传对象

response.write upload.Version&"<br><br>" ''显示上传类的版本

if upload.form("filepath")="" then ''得到上传目录
HtmEnd "请输入要上传至的目录!"
set upload=nothing
response.end
else
formPath=upload.form("filepath")
''在目录后加(/)
if right(formPath,1)<>"/" then formPath=formPath&"/"
end if

iCount=0
for each formName in upload.file ''列出所有上传了的文件
set file=upload.file(formName) ''生成一个文件对象
if file.FileSize>0 then ''如果 FileSize > 0 说明有文件数据
file.SaveAs Server.mappath(formPath&file.FileName) ''保存文件
response.write file.FilePath&file.FileName&" ("&file.FileSize&") => "&formPath&File.FileName&" 成功!<br>"
iCount=iCount+1
end if
set file=nothing
next
set upload=nothing ''删除此对象
Htmend iCount&" 个文件上传结束!"

sub HtmEnd(Msg)
set upload=nothing
response.write "<br>"&Msg&" [<a href=""javascript:history.back();"">返回</a>]</body></html>"
response.end
end sub
%>
</body>
</html>



upload_5xsoft.inc

<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>

'''''''''''''''''''''''''''''''''''''''''''''''''
'
'请保留此信息: 稻香老农制作 http://www.5xSoft.com/
'
'''''''''''''''''''''''''''''''''''''''''''''''''

dim upfile_5xSoft_Stream

Class upload_5xSoft

dim Form,File,Version

Private Sub Class_Initialize
dim iStart,iFileNameStart,iFileNameEnd,iEnd,vbEnter,iFormStart,iFormEnd,theFile
dim strDiv,mFormName,mFormValue,mFileName,mFileSize,mFilePath,iDivLen,mStr
Version="化境编程界HTTP上传程序 Version 1.0"
if Request.TotalBytes<1 then Exit Sub
set Form=CreateObject("Scripting.Dictionary")
set File=CreateObject("Scripting.Dictionary")
set upfile_5xSoft_Stream=CreateObject("Adodb.Stream")
upfile_5xSoft_Stream.mode=3
upfile_5xSoft_Stream.type=1
upfile_5xSoft_Stream.open
upfile_5xSoft_Stream.write Request.BinaryRead(Request.TotalBytes)

vbEnter=Chr(13)&Chr(10)
iDivLen=inString(1,vbEnter)+1
strDiv=subString(1,iDivLen)
iFormStart=iDivLen
iFormEnd=inString(iformStart,strDiv)-1
while iFormStart < iFormEnd
iStart=inString(iFormStart,"name=""")
iEnd=inString(iStart+6,"""")
mFormName=subString(iStart+6,iEnd-iStart-6)
iFileNameStart=inString(iEnd+1,"filename=""")
if iFileNameStart>0 and iFileNameStart<iFormEnd then
iFileNameEnd=inString(iFileNameStart+10,"""")
mFileName=subString(iFileNameStart+10,iFileNameEnd-iFileNameStart-10)
iStart=inString(iFileNameEnd+1,vbEnter&vbEnter)
iEnd=inString(iStart+4,vbEnter&strDiv)
if iEnd>iStart then
mFileSize=iEnd-iStart-4
else
mFileSize=0
end if
set theFile=new FileInfo
theFile.FileName=getFileName(mFileName)
theFile.FilePath=getFilePath(mFileName)
theFile.FileSize=mFileSize
theFile.FileStart=iStart+4
theFile.FormName=FormName
file.add mFormName,theFile
else
iStart=inString(iEnd+1,vbEnter&vbEnter)
iEnd=inString(iStart+4,vbEnter&strDiv)

if iEnd>iStart then
mFormValue=subString(iStart+4,iEnd-iStart-4)
else
mFormValue=""
end if
form.Add mFormName,mFormValue
end if

iFormStart=iformEnd+iDivLen
iFormEnd=inString(iformStart,strDiv)-1
wend
End Sub

Private Function subString(theStart,theLen)
dim i,c,stemp
upfile_5xSoft_Stream.Position=theStart-1
stemp=""
for i=1 to theLen
if upfile_5xSoft_Stream.EOS then Exit for
c=ascB(upfile_5xSoft_Stream.Read(1))
If c > 127 Then
if upfile_5xSoft_Stream.EOS then Exit for
stemp=stemp&Chr(AscW(ChrB(AscB(upfile_5xSoft_Stream.Read(1)))&ChrB(c)))
i=i+1
else
stemp=stemp&Chr(c)
End If
Next
subString=stemp
End function

Private Function inString(theStart,varStr)
dim i,j,bt,theLen,str
InString=0
Str=toByte(varStr)
theLen=LenB(Str)
for i=theStart to upfile_5xSoft_Stream.Size-theLen
if i>upfile_5xSoft_Stream.size then exit Function
upfile_5xSoft_Stream.Position=i-1
if AscB(upfile_5xSoft_Stream.Read(1))=AscB(midB(Str,1)) then
InString=i
for j=2 to theLen
if upfile_5xSoft_Stream.EOS then
inString=0
Exit for
end if
if AscB(upfile_5xSoft_Stream.Read(1))<>AscB(MidB(Str,j,1)) then
InString=0
Exit For
end if
next
if InString<>0 then Exit Function
end if
next
End Function

Private Sub Class_Terminate
form.RemoveAll
file.RemoveAll
set form=nothing
set file=nothing
upfile_5xSoft_Stream.close
set upfile_5xSoft_Stream=nothing
End Sub


Private function GetFilePath(FullPath)
If FullPath <> "" Then
GetFilePath = left(FullPath,InStrRev(FullPath, "\"))
Else
GetFilePath = ""
End If
End function

Private function GetFileName(FullPath)
If FullPath <> "" Then
GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)
Else
GetFileName = ""
End If
End function

Private function toByte(Str)
dim i,iCode,c,iLow,iHigh
toByte=""
For i=1 To Len(Str)
c=mid(Str,i,1)
iCode =Asc(c)
If iCode<0 Then iCode = iCode + 65535
If iCode>255 Then
iLow = Left(Hex(Asc(c)),2)
iHigh =Right(Hex(Asc(c)),2)
toByte = toByte & chrB("&H"&iLow) & chrB("&H"&iHigh)
Else
toByte = toByte & chrB(AscB(c))
End If
Next
End function
End Class


Class FileInfo
dim FormName,FileName,FilePath,FileSize,FileStart
Private Sub Class_Initialize
FileName = ""
FilePath = ""
FileSize = 0
FileStart= 0
FormName = ""
End Sub

Public function SaveAs(FullPath)
dim dr,ErrorChar,i
SaveAs=1
if trim(fullpath)="" or FileSize=0 or FileStart=0 or FileName="" then exit function
if FileStart=0 or right(fullpath,1)="/" then exit function
set dr=CreateObject("Adodb.Stream")
dr.Mode=3
dr.Type=1
dr.Open
upfile_5xSoft_Stream.position=FileStart-1
upfile_5xSoft_Stream.copyto dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=0
end function
End Class
</SCRIPT>


你把上面的稍微改一下就能用了~!
jjdelphi 2001-08-22
  • 打赏
  • 举报
回复
up
wingc 2001-03-09
  • 打赏
  • 举报
回复
ado2.5在win2000中自带,我想把它注册到98或nt4上都不行。
libo02 2001-03-02
  • 打赏
  • 举报
回复
哪有ADO2.5下载?
wingc 2001-03-02
  • 打赏
  • 举报
回复
ado2.5有一个adodb.stream可以操作二进制文件。
haichuang 2001-03-02
  • 打赏
  • 举报
回复
使用CGI当然是小菜一碟,但是要你的提供商只怕不会让你用的。
Itboy 2001-03-02
  • 打赏
  • 举报
回复
php
flyfox 2000-12-22
  • 打赏
  • 举报
回复
谁能告诉我哪个虚拟主机提供商会提供文件上传组件。
或我想还有个办法,用asp以外的办法,如CGI等,能将数据库中的图片写成文件。
那就好办了
wjl2000 2000-09-28
  • 打赏
  • 举报
回复
(1)我最近正在用VB写一个上传文件(文本和图片)的组件,不过尚未调试完成。那位对写组件感兴趣的朋友或高手,欢迎一起切磋。可Email:wjl7348@sohu.com索取源码。
flyfox 2000-09-28
  • 打赏
  • 举报
回复
谁能告诉我哪个虚拟主机提供商会提供文件上传组件。
或我想还有个办法,用asp以外的办法,如CGI等,能将数据库中的图片写成文件。
那就好办了
JJONY 2000-09-11
  • 打赏
  • 举报
回复
有一个问题,不知道大家有没有去想过?如果有人故意上传一个不是图片文件的文件?会有什么结果呢?如果有人把一个EXE文件易名为jpg文件,这会有什么问题出现呢?
flyfox 2000-09-08
  • 打赏
  • 举报
回复
谁能告诉我哪个虚拟主机提供商会提供文件上传组件。
或我想还有个办法,用asp以外的办法,如CGI等,能将数据库中的图片写成文件。
那就好办了
hhzh426 2000-09-01
  • 打赏
  • 举报
回复
有意思!
flyfox 2000-08-28
  • 打赏
  • 举报
回复
谁能告诉我哪个虚拟主机提供商会提供文件上传组件。
或我想还有个办法,用asp以外的办法,如CGI等,能将数据库中的图片写成文件。
那就好办了
subzero 2000-08-24
  • 打赏
  • 举报
回复
要不你干脆写到数据库里得了,反正客户端看到的都一样
flyfox 2000-08-24
  • 打赏
  • 举报
回复
我维护起来不方便。
还是
谁能告诉我哪个虚拟主机提供商会提供文件上传组件。
或我想还有个办法,用asp以外的办法,如CGI等,能将数据库中的图片写成文件。
那就好办了。
mytulip 2000-08-23
  • 打赏
  • 举报
回复
我在ChinaByte上看见一篇文章也许对你有帮助,不过它是保存在DB中,而非文件

用纯ASP代码实现图片上传并存入数据库中
(作者:苏红超 2000年04月26日 11:49)


用ASP编写网站应用程序时间长了,难免会遇到各式各样的问题,其中

关于如何上传文件到服务器恐怕是遇见最多的问题了,尤其是上传图片,比如

你想要在自己的社区里面实现类似网易虚拟社区提供的“每日一星”的功能,

就要提供给网友上传照片的功能。上传图片文件到服务器可以使用各种免费的

文件上传组件,使用起来功能虽然很强大,但是由于很多情况下,我们只能使

用免费的支持ASP的空间或者租用别人的虚拟空间,对于第一种情况,我们

根本就没有可能来使用文件上传组件;至于第二种情况,我们也要付出不少的

“银子”才可以。除非你拥有自己的虚拟主机,你就可以随便的在服务器上面

安装自己所需要的组件,这种情况对于大多数人来说是可望而不可及的。那我

们就没有办法了吗?呵呵,答案是肯定的(当然是肯定的了,要不然我也没法

写出这篇文章啊)。下面就让我们一起来使用纯ASP代码来实现图片的上传

以及保存到数据库的功能(顺便也实现显示数据库中的图片到网页上的功

能)。

首先我们先来熟悉一下将要使用的对象方法。我们用来获取上一个页面传

递过来的数据一般是使用Request对象。同样的,我们也可以使用Request对象

来获取上传上来的文件数据,使用的方法是Request.BinaryRead()。而我们要从

数据库中读出来图片的数据显示到网页上面要用到的方法是:

Request.BinaryWrite()。在我们得到了图片的数据,要保存到数据库中的时候,

不可以直接使用Insert语句对数据库进行操作,而是要使用ADO的

AppendChunk方法,同样的,读出数据库中的图片数据,要使用GetChunk方

法。各个方法的具体语法如下:

* Request.BinaryRead语法:

variant = Request.BinaryRead(count)

参数

variant

返回值保存着从客户端读取到数据。

count

指明要从客户端读取的数据量大小,这个值小于或者等于使用方法

Request.TotalBytes得到的数据量。

* Request.BinaryWrite语法:

Request.BinaryWrite data

参数

data

要写入到客户端浏览器中的数据包。

* Request.TotalBytes语法:

variant = Request.TotalBytes

参数

variant

返回从客户端读取到数据量的字节数。

* AppendChunk语法

将数据追加到大型文本、二进制数据 Field 或 Parameter 对象。

object.AppendChunk Data

参数

object Field 或 Parameter 对象

Data 变体型,包含追加到对象中的数据。

说明

使用 Field 或 Parameter 对象的 AppendChunk 方法可将长二进制或字符数

据填写到对象中。在系统内存有限的情况下,可以使用 AppendChunk 方法对长

整型值进行部分而非全部的操作。

* GetChunk语法

返回大型文本或二进制数据 Field 对象的全部或部分内容 。

variable = field.GetChunk( Size )

返回值

返回变体型。

参数

Size 长整型表达式,等于所要检索的字节或字符数。

说明

使用 Field 对象的 GetChunk 方法检索其部分或全部长二进制或字符数据。

在系统内存有限的情况下,可使用 GetChunk 方法处理部分而非全部的长整型

值。

GetChunk 调用返回的数据将赋给“变量”。如果 Size 大于剩余的数据,则

GetChunk 仅返回剩余的数据而无需用空白填充“变量”。如果字段为空,则

GetChunk 方法返回 Null。

每个后续的 GetChunk 调用将检索从前一次 GetChunk 调用停止处开始的数

据。但是,如果从一个字段检索数据然后在当前记录中设置或读取另一个字段

的值,ADO 将认为已从第一个字段中检索出数据。如果在第一个字段上再次调

用 GetChunk 方法,ADO 将把调用解释为新的 GetChunk 操作并从记录的起始

处开始读取。如果其他 Recordset 对象不是首个 Recordset 对象的副本,则

访问其中的字段不会破坏 GetChunk 操作。

如果 Field 对象的 Attributes 属性中的 adFldLong 位设置为 True,则可

以对该字段使用 GetChunk 方法。

如果在 Field 对象上使用 Getchunk 方法时没有当前记录,将产生错误 3021

(无当前记录)。

接下来,我们就要来设计我们的数据库了,作为测试我们的数据库结构如

下(Access97):

字段名称    类型    描述

  id    自动编号   主键值

img OLE对象   用来保存图片数据 



对于在MS SQL Server7中,对应的结构如下:

字段名称    类型    描述

  id     int(Identity) 主键值

img   image     用来保存图片数据 



现在开始正式编写我们的纯ASP代码上传部分了,首先,我们有一个提

供给用户的上传界面,可以让用户选择要上传的图片。代码如下

(upload.htm):

<html>

<body>

<center>

   <form name="mainForm" enctype="multipart/form-data"

action="process.asp" method=post>

    <input type=file name=mefile><br>

   <input type=submit name=ok value="OK">

   </form>

</center>

</body>

</html>

注意代码中黑色斜体的部分,一定要在Form中有这个属性,否则,将无

法得到上传上来的数据。

接下来,我们要在process.asp中对从浏览器中获取的数据进行必要的处

理,因为我们在process.asp中获取到的数据不仅仅包含了我们想要的上传上来

的图片的数据,也包含了其他的无用的信息,我们需要剔除冗余数据,并将处

理过的图片数据保存到数据库中,这里我们以Access97为例。具体代

码如下(process.asp):

<%

response.buffer=true

formsize=request.totalbytes

formdata=request.binaryread(formsize)

bncrlf=chrB(13) & chrB(10)

divider=leftB(formdata,clng(instrb(formdata,bncrlf))-1)

datastart=instrb(formdata,bncrlf & bncrlf)+4

dataend=instrb(datastart+1,formdata,divider)-datastart

mydata=midb(formdata,datastart,dataend)



set connGraph=server.CreateObject("ADODB.connection")

connGraph.ConnectionString="driver={Microsoft Access Driver (*.mdb)};DBQ=" &

server.MapPath("images.mdb") & ";uid=;PWD=;"

connGraph.Open



set rec=server.createobject("ADODB.recordset")

rec.Open "SELECT * FROM [images] where id is null",connGraph,1,3

rec.addnew

rec("img").appendchunk mydata

rec.update

rec.close

set rec=nothing

set connGraph=nothing

%>

好了,这下我们就把上传来的图片保存到了名为images.mdb的数据库中

了,剩下的工作就是要将数据库中的图片数据显示到网页上面了。一般在HT

ML中,显示图片都是使用<IMG>标签,也就是<IMG SRC="图片路径">,但

是我们的图片是保存到了数据库中,“图片路径”是什么呢?呵呵,其实这个

SRC属性除了指定路径外,也可以这样使用哦:

<IMG SRC="showimg.asp?id=xxx">

所以,我们所要做的就是在showimg.asp中从数据库中读出来符合条件的

数据,并返回到SRC属性中就可以了,具体代码如下(showimg.asp):

<%

set connGraph=server.CreateObject("ADODB.connection")

connGraph.ConnectionString="driver={Microsoft Access Driver (*.mdb)};DBQ=" &

server.MapPath("images.mdb") & ";uid=;PWD=;"

connGraph.Open

set rec=server.createobject("ADODB.recordset")

strsql="select img from images where id=" & trim(request("id"))

rec.open strsql,connGraph,1,1

Response.ContentType = "image/*"

Response.BinaryWrite rec("img").getChunk(7500000)

rec.close

set rec=nothing

set connGraph=nothing

%>

注意在输出到浏览器之前一定要指定Response.ContentType = "image/*",

以便正常显示图片。

最后要注意的地方是,我的process.asp中作的处理没有考虑到第一页

(upload.htm)中还有其他数据,比如<INPUT type=tesxt name=userid>等等,如果

有这些项目,你的process.asp就要注意处理掉不必要的数据。

怎么样,其实上传图片并保存到数据库很简单吧,这样再也不用为自己的

空间无法使用各类的上传组件发愁了吧。还等什么?赶快试一试吧。



flyfox 2000-08-23
  • 打赏
  • 举报
回复
问题是没有虚拟主机提供商会提供文件上传组件,但我又必需此功能,
你说怎么办。
谁能告诉我哪个虚拟主机提供商会提供文件上传组件。
或我想还有个办法,用asp以外的办法,如CGI等,能将数据库中的图片写成文件。
那就好办了。
高手们给个意见吧。
谢谢!
squirrel 2000-08-23
  • 打赏
  • 举报
回复
有何必要呢?如此苦苦追问。

我不清除asp3.0,但我手头的nt4,op4,sp6无法支持在asp中写二进制,所以肯定无法用它写图像文件。
所以只要自己用vb写一个简单的组件就可以解决这个问题了,何必非要把自己憋死不可?

就此祝好,松鼠拜上
squirrel 2000-08-23
  • 打赏
  • 举报
回复
有何必要呢?如此苦苦追问。

我不清除asp3.0,但我手头的nt4,op4,sp6无法支持在asp中写二进制,所以肯定无法用它写图像文件。
所以只要自己用vb写一个简单的组件就可以解决这个问题了,何必非要把自己憋死不可?

就此祝好,松鼠拜上
加载更多回复(17)

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧