上传文件并加入上传信息的实例

TSD 2004-11-08 10:47:36
这段时间看到好几位朋友提问怎样在上传文件的同时加入上传信息的问题,现帖出一个实例代码,希望能帮上有需要的朋友.

上传文件面页:index.htm

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>上传图片</title>
<style type="text/css">
<!--
.style4 {color: #FF0000}
-->
</style>
<script>
//检测图片上传
function checkImage(sId)
{
if(( document.all[sId].value.indexOf(".gif") == -1) && (document.all[sId].value.indexOf(".jpg") == -1)&& (document.all[sId].value.indexOf(".jpeg") == -1)&& (document.all[sId].value.indexOf(".GIF") == -1)&& (document.all[sId].value.indexOf(".JPG") == -1)&& (document.all[sId].value.indexOf(".JPEG") == -1) && (document.all[sId].value.indexOf(".BMP") == -1) && (document.all[sId].value.indexOf(".bmp") == -1)) {
alert("请选择要上传的图片(格式为GIF或JPG)文件!");
document.frmReg.upfile.focus();
event.returnValue = false;
return false
}

if(document.frmReg.txtbt.value.length<1)
{
alert("请输入图片的标题!");
document.frmReg.txtbt.focus();
document.frmReg.txtbt.select();
event.returnValue = false;
return false
}

if(document.frmReg.txtsm.value.length>100)
{
alert("图片的说明不能超过100个字符!");
document.frmReg.txtsm.focus();
event.returnValue = false;
return false
}

}
</script>
</head>
<body>
<p> </p>
<p> </p>
<table width="60%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#003399">
<tr>
<td><table width="100%" border="0" align="center" cellpadding="4" cellspacing="1">
<form name="frmReg" method="post" action="upfile.asp" enctype="multipart/form-data" onsubmit="checkImage('upfile')">
<tr>
<td width="15%" height="30" align="right" bgcolor="#FFFFFF"><strong>文 件:</strong></td>
<td height="30" bgcolor="#FFFFFF"><input name="upfile" type="file" id="upfile" style="width:420" value=""></td>
</tr>
<tr bgcolor="#F7F7F7">
<td height="30" align="right"><strong>标 题:</strong></td>
<td height="30"><input name="txtbt" type="text" id="txtbt" size="20" maxlength="20" style="width:420">
<span class="style4">*</span></td>
</tr>
<tr>
<td height="30" align="right" bgcolor="#FFFFFF"><strong>说 明:</strong></td>
<td height="30" bgcolor="#FFFFFF"><textarea name="txtsm" cols="50" rows="6" id="txtsm" style="width:420"></textarea>
</td>
</tr>
<tr bgcolor="#F6f6f6">
<td height="40" colspan="2" style="padding-left:180px"><input type="submit" name="Submit" style="width:60" value="提交">
  
<input name="Rest" type="reset" id="Rest" style="width:60" value="重置">
</td>
</tr>
</form>
</table></td>
</tr>
</table>
</body>
</html>

连接数据库面页:linkdb.asp
<%
dim conn,connstr,sql
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("Images.mdb")
Set conn=Server.CreateObject("ADODB.Connection")
conn.open connstr
If Err Then
err.Clear
Set conn = Nothing
Response.Write "数据连接错误 !"
Response.End
end if
%>
...全文
960 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
TSD 2005-03-31
  • 打赏
  • 举报
回复
补充一点,这个上传的文件类型还要做一些处理,进行验证

'验证文件类型函数
function vfile(fx)
if fx=".asp" then
response.write "对不起,不能上传这一类文件!"
response.end
else
vfile=fx
end if
end function


thename=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&right(file.filename,4)

改为:

thename=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&vfile(right(file.filename,4))
txx007 2005-03-17
  • 打赏
  • 举报
回复
真是太好了,谢谢!
zeoyeah 2005-02-24
  • 打赏
  • 举报
回复
我按这里做,还是会出错,帮忙
ADODB.Stream (0x800A0BBC)
/uppic/upload.inc, 第 175 行
TSD 2004-11-08
  • 打赏
  • 举报
回复
数据库名:Images.mdb
表名:up_Images
字段:

i_ID(自动ID)
i_Title(文本)
i_Url(文本)
i_Rem(文本)
TSD 2004-11-08
  • 打赏
  • 举报
回复
上传组件文件面页:upload.inc
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>

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=""
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>
TSD 2004-11-08
  • 打赏
  • 举报
回复
后台上传文件:upfile.asp
<!--#include FILE="linkdb.asp"-->
<!--#include FILE="upload.inc"-->
<%
dim upload,file,formName,formPath,iCount
dim bt,sm
set upload=new upload_5xSoft
'以下两行获得上传的信息
bt=upload.Form("txtbt")
sm=upload.Form("txtsm")
'这个是要上传的路径
formPath="Images/"
for each formName in upload.file
set file=upload.file(formName)
if file.FileSize>0 and file.FileSize<400*1024 then
dim thename,fsobj,spp,paths
thename=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&right(file.filename,4)
spp=file.filename
file.filename=thename
file.SaveAs Server.mappath(formPath&file.FileName)
set fsobj=nothing
'将上传的图片信息添加到数据库中
sql="insert into up_images(i_Title,i_Url,i_Rem) values('" & bt & "','" & formPath & file.filename & "','" & sm & "')"
conn.Execute (sql)
set conn=nothing
response.write "<br><br><br><br><br><center>上传成功!<a href=index.htm>继续上传</a></center>"
else
response.write "<br><br><br><br><br><center>上传失败<a href=index.htm>返回</a></center>"
response.end
end if
set file=nothing
next
set upload=nothing
%>

28,391

社区成员

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

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