求助:请问如何用ASP上传图片及文件到啊

Jsong6622 2004-11-22 09:32:22
我是asp编程初学者,请问如何实现在线上传图片及文件啊。
...全文
208 点赞 收藏 18
写回复
18 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Jsong6622 2004-11-23
再次谢谢各们对我帮助,问题也顺利解决了
谢谢
回复
angelyujun 2004-11-23
upload_5xsoft.inc

<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
dim Data_5xsoft

Class upload_5xsoft

dim objForm,objFile,Version

Public function Form(strForm)
strForm=lcase(strForm)
if not objForm.exists(strForm) then
Form=""
else
Form=objForm(strForm)
end if
end function

Public function File(strFile)
strFile=lcase(strFile)
if not objFile.exists(strFile) then
set File=new FileInfo
else
set File=objFile(strFile)
end if
end function


Private Sub Class_Initialize
dim RequestData,sStart,vbCrlf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,theFile
dim iFileSize,sFilePath,sFileType,sFormValue,sFileName
dim iFindStart,iFindEnd
dim iFormStart,iFormEnd,sFormName
Version="化境HTTP上传程序 Version 2.0"
set objForm=Server.CreateObject("Scripting.Dictionary")
set objFile=Server.CreateObject("Scripting.Dictionary")
if Request.TotalBytes<1 then Exit Sub
set tStream = Server.CreateObject("adodb.stream")
set Data_5xsoft = Server.CreateObject("adodb.stream")
Data_5xsoft.Type = 1
Data_5xsoft.Mode =3
Data_5xsoft.Open
Data_5xsoft.Write Request.BinaryRead(Request.TotalBytes)
Data_5xsoft.Position=0
RequestData =Data_5xsoft.Read

iFormStart = 1
iFormEnd = LenB(RequestData)
vbCrlf = chrB(13) & chrB(10)
sStart = MidB(RequestData,1, InStrB(iFormStart,RequestData,vbCrlf)-1)
iStart = LenB (sStart)
iFormStart=iFormStart+iStart+1
while (iFormStart + 10) < iFormEnd
iInfoEnd = InStrB(iFormStart,RequestData,vbCrlf & vbCrlf)+3
tStream.Type = 1
tStream.Mode =3
tStream.Open
Data_5xsoft.Position = iFormStart
Data_5xsoft.CopyTo tStream,iInfoEnd-iFormStart
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sInfo = tStream.ReadText
tStream.Close
'取得表单项目名称
iFormStart = InStrB(iInfoEnd,RequestData,sStart)
iFindStart = InStr(22,sInfo,"name=""",1)+6
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFormName = lcase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
'如果是文件
if InStr (45,sInfo,"filename=""",1) > 0 then
set theFile=new FileInfo
'取得文件名
iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
theFile.FileName=getFileName(sFileName)
theFile.FilePath=getFilePath(sFileName)
'取得文件类型
iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14
iFindEnd = InStr(iFindStart,sInfo,vbCr)
theFile.FileType =Mid (sinfo,iFindStart,iFindEnd-iFindStart)
theFile.FileStart =iInfoEnd
theFile.FileSize = iFormStart -iInfoEnd -3
theFile.FormName=sFormName
if not objFile.Exists(sFormName) then
objFile.add sFormName,theFile
end if
else
'如果是表单项目
tStream.Type =1
tStream.Mode =3
tStream.Open
Data_5xsoft.Position = iInfoEnd
Data_5xsoft.CopyTo tStream,iFormStart-iInfoEnd-3
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sFormValue = tStream.ReadText
tStream.Close
if objForm.Exists(sFormName) then
objForm(sFormName)=objForm(sFormName)&", "&sFormValue
else
objForm.Add sFormName,sFormValue
end if
end if
iFormStart=iFormStart+iStart+1
wend
RequestData=""
set tStream =nothing
End Sub

Private Sub Class_Terminate
if Request.TotalBytes>0 then
objForm.RemoveAll
objFile.RemoveAll
set objForm=nothing
set objFile=nothing
Data_5xsoft.Close
set Data_5xsoft =nothing
end if
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
End Class

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

Public function SaveAs(FullPath)
dim dr,ErrorChar,i
SaveAs=true
if trim(fullpath)="" or FileStart=0 or FileName="" or right(fullpath,1)="/" then exit function
set dr=CreateObject("Adodb.Stream")
dr.Mode=3
dr.Type=1
dr.Open
Data_5xsoft.position=FileStart
Data_5xsoft.copyto dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=false
end function
End Class
</SCRIPT>
回复
angelyujun 2004-11-23
研究一下这两个文件

add_photo.asp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--#include file="upload_5xsoft.inc"-->
<%

function Get_FileType(s)
dim intFindStart
dim intFindLen
intFindStart=instr(s,".")
intFindLen=len(s)-intFindStart
Get_FileType=Mid(s,intFindStart+1,intFindLen)
end function

%>
<%
if request("method")="add" then
set upload=new upload_5xsoft
'是否有图片
if upload.file("pic1").filesize=0 or upload.file("pic1").filesize>=200*1024 then
response.write ("<script language='javascript'>alert('无图片或图片太大!');location='javascript:history.go(-1)';</script>")
response.end()
end if
'图片类型
filename=upload.file("pic1").filename
if lcase(Get_FileType(filename))<>"jpg" and lcase(Get_FileType(filename))<>"gif" then
response.write ("<script language='javascript'>alert('请注意图片格式!');location='javascript:history.go(-1)';</script>")
response.end()
end if
'图片名保存数据库
strSaveFileName=upload.form("id")&year(now())&month(now())&day(now())&hour(now())&minute(now())&second(now())&"."&Get_FileType(filename)
sql="insert into home_photo(pic) values('"&strSaveFileName&"')"
'conn.execute sql
'图片上传文件夹
upload.file("pic1").saveAs Server.mappath("../photos/images/"&strSaveFileName) '索引图片存放路径
response.write ("<script language='javascript'>alert('添加成功!');</script><meta HTTP-EQUIV=refresh Content=0;url='del_photo.asp'>")
end if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="css.css" type="text/css">
<title>Untitled Document</title>
<link rel="stylesheet" href="css.css" type="text/css">

<body>
<form action="add_photo.asp?method=add" method="post" enctype="multipart/form-data">
<table width="98%" border="1" cellpadding="0" cellspacing="0" bordercolor="#FF9900" bgcolor="#F9E4D9" class="thin">
<tr>
<td width="100" height="25" align="right">照片:</font></td>
<td bgcolor="#F9E4D9"><input name="pic1" type="file" id="pic1">  **</td>
</tr>
<tr>
<td width="100" height="25" align="right"> </td>
<td bgcolor="#F9E4D9">
<input name="submit" type="submit" id="submit" value="添加完成"><br><br>
<font style="font-size:12px; color:#FF0000 ">
图片大小不能超过200kb,格式只能为gif或jpg格式,索引图片和原始图片格式必须一致。
</font>
</td>
</tr>
</table>
</form>
</body>
</html>
回复
Jsong6622 2004-11-23
谢谢各们对我的帮助,化境无组件上传确实很好用。
但对于一个像我这样初学者而言,总觉得一些基础问题还是弄清楚的较好虽说这些东西以后或许用的很少但总归是打基础。像搂上的随风给我贴的代码我认认真真地做了一遍可显示结果却是乱码。还请大侠们帮我指点指点我的错在那儿啊
回复
yb2008 2004-11-22
永久不删除
回复
yb2008 2004-11-22
有这个吧:


http://www.hc189.com/upload.rar 化境组件
回复
gu1dai 2004-11-22
用我的修改版吧
http://www.websamba.com/gudai/updown
回复
clcy 2004-11-22
请大家抓紧时间下载三天后将删除
回复
clcy 2004-11-22
ASP无组件上传到数据库加读出图片

http://www.uniclr.com/end/clcy.rar
回复
gu1dai 2004-11-22
我有个化境修改版,可以对上传后的文件进行管理
你要我就发给你base5@126.com
回复
TSD 2004-11-22
http://community.csdn.net/Expert/topic/3568/3568110.xml?temp=.2747614
回复
TSD 2004-11-22
http://community.csdn.net/Expert/TopicView.asp?id=3531185
回复
everywhen 2004-11-22
使用组件,化境的还可以,简单
回复
jasonboy 2004-11-22
不行的话,你还是找组件吧,简单,省事
回复
bearzhan88 2004-11-22
上传文件要看你在服务器后台是如何保存这些数据。如果用文件格式保存,只要用FSO就可以了,如果用数据库存,就要分 哪一种数据库。(文件格式存放的我就不说了)
现在流行的 化境、无惧等无组件上传 都是 应用在ORACLE、SQL库下,应用原理都是 读取 数据流 STREAM,然后用 APEENDCHUNK()写入到数据库的BLOB中。 DB2 用不了APPENDCHUNK(),郁闷吧,如果你在DB2环境开发的话,老老实实的灵活点,数据库存文件名,文件数据用FSO建立文件,保存成文件吧。
回复
tongyi2005 2004-11-22
注意:化境ASP无组件上传类会被某些杀毒软件认为木马的.

建议用: http://www.ccopus.com/code/upload.html
回复
fang_qing 2004-11-22
贴个代码给你:

---------------------file上传页面---------------------
<form method="POST" action="up.asp">
<p><input type="file" name="T1" size="20"><input type="submit" value="提交" name="B1"></p>
</form>

---------------------上传到数据库(up.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 conn=Server.CreateObject("ADODB.Connection")
conn.open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("数据库名.mdb")
set rec=server.createobject("ADODB.recordset")
rec.Open "SELECT * from 表名 where id is null",conn,1,3
rec.addnew
rec("img").appendchunk mydata
rec.update
rec.close
set rec=nothing
set connGraph=nothing
%>
--------------------将数据库中的数据显示(xs.asp)--------------------
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("数据库名.mdb")
set rs = server.CreateObject("ADODB.Recordset")
sql="select * from 表名 where id="&request("id")
rs.open sql,conn,1,3
Response.ContentType = "image/*"
Response.BinaryWrite rs("img").getChunk(7500000)
rs.close
set rs=nothing
set conn=nothing
%>
回复
zzy0000 2004-11-22
化境ASP无组件上传类
http://www.5xsoft.com/

你去下载看看,里面有说明书,挺详细的
回复
相关推荐
发帖
ASP
创建于2007-09-28

2.8w+

社区成员

ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
申请成为版主
帖子事件
创建了帖子
2004-11-22 09:32
社区公告
暂无公告