分离一段代码 。简单的上传附件,点击留言并上传。然后把路径写入数据库
全部代码:
上传页面代码:
上传稿件
<input name="img" type="hidden" id="img" value="nopic.gif">
<iframe src="../upload.asp" width="100%" height="30" frameborder="0" scrolling="no"></iframe>
入库字段 (记录字段为 up )
rs("up") =trim(Request("img"))
包含文件:
upload.asp
<!--#include file="inc/up.asp"-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
BODY{
font-size:9pt;
background-color: #d4d0c8;
}
-->
</style>
<script language="javascript">
function change_path()
{
var obj=document.all.image1;
var file_path = document.all.file_path
//alert(file_path.value);
obj.src=file_path.value;
}
//让iframe自实适大小
function window.onload()
{
if(top.location != self.location)
{
var a = window.parent.document.getElementsByTagName('iframe');
for (var i=0; i<a.length; i++)
{
if (a[i].name == self.name)
{
a[i].height = document.body.scrollHeight+10;
return;
}
}
}
}
</script>
<title>上传文件</title>
</head>
<body leftmargin="0" topmargin="0">
<%
if EnableUploadFile="Yes" then
%>
<form action="upload_class.asp" method="post" name="form1" enctype="multipart/form-data">
<table width="100%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td><input name="file_path" type="FILE" size="20" onChange="change_path()">
<input type="submit" name="Submit" value="上传"> </td>
</tr>
</table>
</form>
<%
elseif EnableUploadFile="No" then response.Write"网站系统关闭了文件上传功能"
'else
'response.Write"登陆后才能使用文件上传功能"
end if
%>
</body>
</html>
inc/up.asp
<%
Const EnableUploadFile="Yes" '是否开放文件上传
Const MaxFileSize=200 '上传文件大小限制
Const SaveUpFilesPath="UploadFiles" '存放上传文件的目录
Const UpFileType="doc" '允许的上传文件类型
Const DelUpFiles="Yes" '删除文章时是否同时删除文章中的上传文件
%>
upload_class.asp
<!--#include file="inc/up.asp"-->
<!--#include file="upload.inc"-->
<%
const upload_type=0 '上传方法:0=无惧无组件上传类,1=FSO上传 2=lyfupload,3=aspupload,4=chinaaspupload
dim upload,file,formName,SavePath,filename,FileAdd,fileExt
dim upNum
dim EnableUpload
dim Forumupload
dim ranNum
dim uploadfiletype
dim msg,founderr
msg=""
founderr=false
EnableUpload=false
SavePath = SaveUpFilesPath '存放上传文件的目录
SavePath = Replace(SavePath,Chr(0),"")
if right(SavePath,1)<>"/" then SavePath=SavePath&"/" '在目录后加(/)
ComeinSTR=lcase(request.servervariables("HTTP_HOST"))
Url=split(ComeinSTR)
yourthing=Url(0)
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="user/style.css" rel="stylesheet" type="text/css">
<script>
function window.onload()
{
if(top.location != self.location)
{
var a = window.parent.document.getElementsByTagName('iframe');
for (var i=0; i<a.length; i++)
{
if (a[i].name == self.name)
{
a[i].height = document.body.scrollHeight+10;
return;
}
}
}
}
</script>
</head>
<body>
<%
if EnableUploadFile="No" then
response.write "系统未开放文件上传功能"
else
'if Trim(Request.Cookies("zzcms")("UserName"))<>"" or session("admin")<>"" then
select case upload_type
case 0
call upload_0() '使用化境无组件上传类
case else
'response.write "本系统未开放插件功能"
'response.end
end select
'else
'response.Write("请登录后再使用本功能!")
'response.end()
'end if
end if
%>
<%if fileExt="swf" then%>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0">
<param name="movie" value="<%=fileAdd%>">
<param name="quality" value="high">
<embed src="<%=fileAdd%>" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed></object>
<%else%>
<a href="<%=fileAdd%>" target="_blank"><img src="<%=fileAdd%>" border="0" onload="javascript:if(this.width>150) this.width=150;"></a>
<%end if%>
</body>
</html>
<%
'-----------------------------------------------------------------------------------
'格式后缀
'-----------------------------------------------------------------------------------
Private Function FixName(Byval UpFileExt)
If IsEmpty(UpFileExt) Then Exit Function
FixName = Lcase(UpFileExt)
FixName = Replace(FixName,Chr(0),"")
FixName = Replace(FixName,".","")
FixName = Replace(FixName,"'","")
FixName = Replace(FixName,"asp","")
FixName = Replace(FixName,"asa","")
FixName = Replace(FixName,"aspx","")
FixName = Replace(FixName,"cer","")
FixName = Replace(FixName,"cdx","")
FixName = Replace(FixName,"htr","")
End Function
'-----------------------------------------------------------------------------------
'判断文件类型是否合格
'-----------------------------------------------------------------------------------
Private Function CheckFileExt(FileExt)
Dim Forumupload,i
CheckFileExt=False
If FileExt="" or IsEmpty(FileExt) Then
CheckFileExt = False
Exit Function
End If
If FileExt="asp" or FileExt="asa" or FileExt="aspx" Then
CheckFileExt = False
Exit Function
End If
Forumupload = Split(UpFileType,"|")
For i = 0 To ubound(Forumupload)
If FileExt = Trim(Forumupload(i)) Then
CheckFileExt = True
Exit Function
Else
CheckFileExt = False
End If
Next
End Function
'使用化境无组件上传类
sub upload_0()
set upload=new upload_file '建立上传对象
for each formName in upload.file '列出所有上传了的文件
set file=upload.file(formName) '生成一个文件对象
if file.filesize<1 then
'msg="请先选择你要上传的文件!"
response.write"<SCRIPT language='JavaScript'>alert('请点击“浏览”,先选择您要上传的文件!');"'这样修改为了弹出信息后,再能返回到上传页面。
response.write"javascript:history.go(-1)</SCRIPT>"
end if
if file.filesize>(MaxFileSize*1024) then
'msg="文件大小超过了限制,最大只能上传" & CStr(MaxFileSize) & "K的文件!"
response.write"<SCRIPT language='JavaScript'>alert('文件大小超过了限制!最大只能上传" & CStr(MaxFileSize) & "K的文件!');"
response.write"javascript:history.go(-1)</SCRIPT>"
end if
FileExt = FixName(File.FileExt)
If CheckFileExt(FileExt) = False then
response.write"<SCRIPT language=JavaScript>alert('这种文件类型不允许上传!\n\n只允许上传这几种文件类型:" & UpFileType & "');"
response.write"javascript:history.go(-1)</SCRIPT>"
founderr=true
EXIT SUB
End If
strJS="<SCRIPT language=javascript>" & vbcrlf
if founderr<>true then
set f=server.CreateObject("scripting.filesystemobject")
y=year(date)
m=month(date)
if len(m)<2 then m="0"&m
folderPath = y&m&"/"
folderName =server.mappath("UploadFiles/")&"\"&y&m&"\"
if not f.folderexists(folderName) then f.createfolder(folderName)
set f=nothing
randomize
ranNum=int(900*rnd)+100
FileName=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum&"."&fileExt
'filename=SavePath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum&"."&fileExt
'用常量防止被截断
fileAdd="uploadfiles/"&folderPath&FileName
file.SaveToFile Server.mappath(fileAdd) '保存文件
msg="上传文件成功!"
strJS=strJS & "parent.myform.img.value='" & folderPath&FileName & "';" & vbcrlf
end if
strJS=strJS & "alert('" & msg & "');" & vbcrlf
strJS=strJS & "</script>"
response.write strJS
set file=nothing
next
set upload=nothing
end sub
%>
ok 。分离完毕,照葫芦画瓢即可。
转载于:https://my.oschina.net/delmore/blog/4668