高分求ASP上传文件代码

求一个能上传文件的简单ASP代码(VBScript)要求:上传的文件存放在服务器的文件夹中同时要把文件的路径存放到ACCESS数据库中(连接之类不用写)以便前台下载能根据... 求一个能上传文件的简单ASP代码(VBScript)
要求:
上传的文件存放在服务器的文件夹中
同时要把文件的路径存放到ACCESS数据库中(连接之类不用写)
以便前台下载
能根据上传时的选项存放于不同文件夹再+50
TO: mcs8714520
不是图片,是文件,且不用组件,其实我只是不会得到上传文件名
大家要是能给出得到文件名的代码也照样给分。但请尽量简单。
TO: colourfans
那个我以前有试过,的确可以传,但就连上传到本地服务器都很慢,
上传到网站上就更慢了
TO: 惊窒而息
可以+50分的~只要代码好,重要的是简单而有效
TO2: colourfans
用化境上传成功后显示是乱码,在inc文件里把GB2312改成别的编码也不行
请告诉我怎么弄?
T02:惊窒而息
那个不知道怎么弄哦,有两个Text,还有两个提交按钮
展开
 我来答
惊窒而息
2008-09-19 · TA获得超过126个赞
知道小有建树答主
回答量:212
采纳率:0%
帮助的人:237万
展开全部
不多说了,我把源码传上网了,你可以打开 http://tonv.b67.53dns.cn 传文件看看是不是你要的效果。 http://tonv.b67.53dns.cn/view.asp 是文件显示页面,我已经把源码打包传上去了,编号16的就是,直接点“点击打开”就可以下载

补充:第一个提交是上传文件的提交,此时提交后文件会被成功上传且把路径和文件名写入到<input name="content">这个文本域内,也就是你提交到数据库的字段
第二个提交是提交index.asp表单的,也就是你自己要提交到数据库里的表单。简而言之只要你将代码弄好了,这个提交就会保存文件名到数据库,存入数据库后要显示文件名和路径就可以调用<%=rs("content")>,content是你数据库存放上传文件名和路径的字段名!你再试试,不会我发完整源码给你,你如果想上传的时候同时提交到数据库可以在upload.asp页面用个隐藏表单。

另外第一个文本域是我测试的时候上传到数据库的另外一个字段title,可以不用管,关键部分在第二个文本域,是取得上传文件名和路径的字段

****************************************************************

嘿,都是骗分的嚒,看清楚了楼主要显示上传文件名并写入数据库,没问题,看我的: (有四个asp文件,别把代码复制到一个页面中了,有什么问题请补充!如果还不会请告诉我,我把源码打包发给你!)

UpLoadClass.asp,主要上传程序,代码如下:
<%
Class UpLoadClass

Private p_MaxSize,p_TotalSize,p_FileType,p_SavePath,p_AutoSave,p_Error
Private objForm,binForm,binItem,strDate,lngTime
Public FormItem,FileItem

Public Property Get Version
Version="Rumor UpLoadClass Version 2.0B"
End Property

Public Property Get Error
Error=p_Error
End Property

Public Property Get MaxSize
MaxSize=p_MaxSize
End Property
Public Property Let MaxSize(lngSize)
if isNumeric(lngSize) then
p_MaxSize=clng(lngSize)
end if
End Property

Public Property Get TotalSize
TotalSize=p_TotalSize
End Property
Public Property Let TotalSize(lngSize)
if isNumeric(lngSize) then
p_TotalSize=clng(lngSize)
end if
End Property

Public Property Get FileType
FileType=p_FileType
End Property
Public Property Let FileType(strType)
p_FileType=strType
End Property

Public Property Get SavePath
SavePath=p_SavePath
End Property
Public Property Let SavePath(strPath)
p_SavePath=replace(strPath,chr(0),"")
End Property

Public Property Get AutoSave
AutoSave=p_AutoSave
End Property
Public Property Let AutoSave(byVal Flag)
select case Flag
case 0:
case 1:
case 2:
case false:Flag=2
case else:Flag=0
end select
p_AutoSave=Flag
End Property

Private Sub Class_Initialize
p_Error = -1
p_MaxSize = 153600
p_FileType = "jpg/gif"
p_SavePath = ""
p_AutoSave = 0
p_TotalSize= 0
strDate = replace(cstr(Date()),"-","")
lngTime = clng(timer()*1000)
Set binForm = Server.CreateObject("ADODB.Stream")
Set binItem = Server.CreateObject("ADODB.Stream")
Set objForm = Server.CreateObject("Scripting.Dictionary")
objForm.CompareMode = 1
End Sub

Private Sub Class_Terminate
objForm.RemoveAll
Set objForm = nothing
Set binItem = nothing
if p_Error<>4 then binForm.Close()
Set binForm = nothing
End Sub

Public Sub Open()
if p_Error=-1 then
p_Error=0
else
Exit Sub
end if
Dim lngRequestSize,binRequestData,strFormItem,strFileItem
Const strSplit="'"">"
lngRequestSize=Request.TotalBytes
if lngRequestSize<1 or (lngRequestSize>p_TotalSize and p_TotalSize<>0) then
p_Error=4
Exit Sub
end if
binRequestData=Request.BinaryRead(lngRequestSize)
binForm.Type = 1
binForm.Open
binForm.Write binRequestData

Dim bCrLf,strSeparator,intSeparator
bCrLf=ChrB(13)&ChrB(10)

intSeparator=InstrB(1,binRequestData,bCrLf)-1
strSeparator=LeftB(binRequestData,intSeparator)

Dim p_start,p_end,strItem,strInam,intTemp,strTemp
Dim strFtyp,strFnam,strFext,lngFsiz
p_start=intSeparator+2
Do
p_end =InStrB(p_start,binRequestData,bCrLf&bCrLf)+3
binItem.Type=1
binItem.Open
binForm.Position=p_start
binForm.CopyTo binItem,p_end-p_start
binItem.Position=0
binItem.Type=2
binItem.Charset="gb2312"
strItem=binItem.ReadText
binItem.Close()

p_start=p_end
p_end =InStrB(p_start,binRequestData,strSeparator)-1
binItem.Type=1
binItem.Open
binForm.Position=p_start
lngFsiz=p_end-p_start-2
binForm.CopyTo binItem,lngFsiz

intTemp=Instr(39,strItem,"""")
strInam=Mid(strItem,39,intTemp-39)

if Instr(intTemp,strItem,"filename=""")<>0 then
if not objForm.Exists(strInam&"_From") then
strFileItem=strFileItem&strSplit&strInam
if binItem.Size<>0 then
intTemp=intTemp+13
strFtyp=Mid(strItem,Instr(intTemp,strItem,"Content-Type: ")+14)
strTemp=Mid(strItem,intTemp,Instr(intTemp,strItem,"""")-intTemp)
intTemp=InstrRev(strTemp,"\")
strFnam=Mid(strTemp,intTemp+1)
objForm.Add strInam&"_Type",strFtyp
objForm.Add strInam&"_Name",strFnam
objForm.Add strInam&"_Path",Left(strTemp,intTemp)
objForm.Add strInam&"_Size",lngFsiz
if Instr(intTemp,strTemp,".")<>0 then
strFext=Mid(strTemp,InstrRev(strTemp,".")+1)
else
strFext=""
end if
if left(strFtyp,6)="image/" then
binItem.Position=0
binItem.Type=1
strTemp=binItem.read(10)
if strcomp(strTemp,chrb(255) & chrb(216) & chrb(255) & chrb(224) & chrb(0) & chrb(16) & chrb(74) & chrb(70) & chrb(73) & chrb(70),0)=0 then
if Lcase(strFext)<>"jpg" then strFext="jpg"
binItem.Position=3
do while not binItem.EOS
do
intTemp = ascb(binItem.Read(1))
loop while intTemp = 255 and not binItem.EOS
if intTemp < 192 or intTemp > 195 then
binItem.read(Bin2Val(binItem.Read(2))-2)
else
Exit do
end if
do
intTemp = ascb(binItem.Read(1))
loop while intTemp < 255 and not binItem.EOS
loop
binItem.Read(3)
objForm.Add strInam&"_Height",Bin2Val(binItem.Read(2))
objForm.Add strInam&"_Width",Bin2Val(binItem.Read(2))
elseif strcomp(leftB(strTemp,8),chrb(137) & chrb(80) & chrb(78) & chrb(71) & chrb(13) & chrb(10) & chrb(26) & chrb(10),0)=0 then
if Lcase(strFext)<>"png" then strFext="png"
binItem.Position=18
objForm.Add strInam&"_Width",Bin2Val(binItem.Read(2))
binItem.Read(2)
objForm.Add strInam&"_Height",Bin2Val(binItem.Read(2))
elseif strcomp(leftB(strTemp,6),chrb(71) & chrb(73) & chrb(70) & chrb(56) & chrb(57) & chrb(97),0)=0 or strcomp(leftB(strTemp,6),chrb(71) & chrb(73) & chrb(70) & chrb(56) & chrb(55) & chrb(97),0)=0 then
if Lcase(strFext)<>"gif" then strFext="gif"
binItem.Position=6
objForm.Add strInam&"_Width",BinVal2(binItem.Read(2))
objForm.Add strInam&"_Height",BinVal2(binItem.Read(2))
elseif strcomp(leftB(strTemp,2),chrb(66) & chrb(77),0)=0 then
if Lcase(strFext)<>"bmp" then strFext="bmp"
binItem.Position=18
objForm.Add strInam&"_Width",BinVal2(binItem.Read(4))
objForm.Add strInam&"_Height",BinVal2(binItem.Read(4))
end if
end if
objForm.Add strInam&"_Ext",strFext
objForm.Add strInam&"_From",p_start
intTemp=GetFerr(lngFsiz,strFext)
if p_AutoSave<>2 then
objForm.Add strInam&"_Err",intTemp
if intTemp=0 then
if p_AutoSave=0 then
strFnam=GetTimeStr()
if strFext<>"" then strFnam=strFnam&"."&strFext
end if
binItem.SaveToFile Server.MapPath(p_SavePath&strFnam),2
objForm.Add strInam,strFnam
end if
end if
else
objForm.Add strInam&"_Err",-1
end if
end if
else
binItem.Position=0
binItem.Type=2
binItem.Charset="gb2312"
strTemp=binItem.ReadText
if objForm.Exists(strInam) then
objForm(strInam) = objForm(strInam)&","&strTemp
else
strFormItem=strFormItem&strSplit&strInam
objForm.Add strInam,strTemp
end if
end if

binItem.Close()
p_start = p_end+intSeparator+2
loop Until p_start+3>lngRequestSize
FormItem=split(strFormItem,strSplit)
FileItem=split(strFileItem,strSplit)
End Sub

Private Function GetTimeStr()
lngTime=lngTime+1
GetTimeStr=strDate&lngTime
End Function

Private Function GetFerr(lngFsiz,strFext)
dim intFerr
intFerr=0
if lngFsiz>p_MaxSize and p_MaxSize>0 then
if p_Error=0 or p_Error=2 then p_Error=p_Error+1
intFerr=intFerr+1
end if
if Instr(1,LCase("/"&p_FileType&"/"),LCase("/"&strFext&"/"))=0 and p_FileType<>"" then
if p_Error<2 then p_Error=p_Error+2
intFerr=intFerr+2
end if
GetFerr=intFerr
End Function

Public Function Save(Item,strFnam)
Save=false
if objForm.Exists(Item&"_From") then
dim intFerr,strFext
strFext=objForm(Item&"_Ext")
intFerr=GetFerr(objForm(Item&"_Size"),strFext)
if objForm.Exists(Item&"_Err") then
if intFerr=0 then
objForm(Item&"_Err")=0
end if
else
objForm.Add Item&"_Err",intFerr
end if
if intFerr<>0 then Exit Function
if VarType(strFnam)=2 then
select case strFnam
case 0:strFnam=GetTimeStr()
if strFext<>"" then strFnam=strFnam&"."&strFext
case 1:strFnam=objForm(Item&"_Name")
end select
end if
binItem.Type = 1
binItem.Open
binForm.Position = objForm(Item&"_From")
binForm.CopyTo binItem,objForm(Item&"_Size")
binItem.SaveToFile Server.MapPath(p_SavePath&strFnam),2
binItem.Close()
if objForm.Exists(Item) then
objForm(Item)=strFnam
else
objForm.Add Item,strFnam
end if
Save=true
end if
End Function

Public Function GetData(Item)
GetData=""
if objForm.Exists(Item&"_From") then
if GetFerr(objForm(Item&"_Size"),objForm(Item&"_Ext"))<>0 then Exit Function
binForm.Position = objForm(Item&"_From")
GetData=binFormStream.Read(objForm(Item&"_Size"))
end if
End Function

Public Function Form(Item)
if objForm.Exists(Item) then
Form=objForm(Item)
else
Form=""
end if
End Function

Private Function BinVal2(bin)
dim lngValue,i
lngValue = 0
for i = lenb(bin) to 1 step -1
lngValue = lngValue *256 + ascb(midb(bin,i,1))
next
BinVal2=lngValue
End Function

Private Function Bin2Val(bin)
dim lngValue,i
lngValue = 0
for i = 1 to lenb(bin)
lngValue = lngValue *256 + ascb(midb(bin,i,1))
next
Bin2Val=lngValue
End Function

End Class
%>
****************************************************

upload.asp 上传设置与调用文件,代码如下:
<!--#include FILE="UpLoadClass.asp"-->
<%
dim request2,ming
'建立上传对象
set request2=New UpLoadClass
'上传总大小为20M
request2.TotalSize= 20971520
'单文件最大10M
request2.MaxSize = 10485760
'允许上传的文件格式
request2.FileType = "rar/zip/jpg"
'设置上传文件的目录,使用相对路径,注意设置的目录要存在,不然会出错,如果为设置为空则文件上传于根目录下
request2.SavePath="upload/"
request2.open()
'设置ming这个变量等于上传文件的路径和文件名
ming = request2.SavePath&request2.Form("strFile1")
'在提交数据库页面把文件路径和文件名写入表单以供提交
response.write "<script>parent.document.form1.content.value='" & ming & "'</script>"
%>
*************************************************
up.asp,上传提交表单页面,代码如下
<form action="upload.asp" method="post" enctype="multipart/form-data" name="form">
<input type="file" name="strFile1" id="strFile1">
<input type="submit" name="Submit" value="提交">
</form>
*************************************************
index.asp 你自己的提交页面,我只写表单部分的代码
<form name="form1" method="POST" action="">
<input name="title" type="text" id="title">
<br><br>
<input name="content" type="text" id="content">
<br><iframe src="up.asp" name="up" width="300" marginwidth="0" height="30" marginheight="0" align="middle" scrolling="no" frameborder="0"></iframe>
<input type="submit" name="Submit" value="上传">
<br>
<input type="hidden" name="MM_insert" value="form1">
</form>

OK,完工了,这里用的是风声无组件上传,根据你的需要修改成你要的版本,还可以多文件上传的,你可以下一个风声的源码研究看看

另外,程序的主要你要的部分就是upload.asp中的:response.write "<script>parent.document.form1.content.value='" & ming & "'</script>" 这个是把上传得到的文件名和路径写入你要提交的表单,还有你自己的页面要用<iframe>调用up.asp。还有什么不懂的可以QQ问我:54660142

参考资料: http://tonv.b67.53dns.cn

koumenglin
2008-09-19 · TA获得超过643个赞
知道小有建树答主
回答量:1250
采纳率:0%
帮助的人:779万
展开全部
<%OPTION EXPLICIT%>
<%Server.ScriptTimeOut=5000%>
<!--#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.objForm ''列出所有form数据
? response.write formName&"="&upload.form(formName)&"<br>"
next

response.write "<br>"
for each formName in upload.objFile ''列出所有上传了的文件
? 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>
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友c44dc36
2008-09-19 · TA获得超过495个赞
知道小有建树答主
回答量:411
采纳率:0%
帮助的人:0
展开全部
http://www.5xsoft.com/upload.html
著名的 化境编程界无组件上传
就是过去的稻乡老农
去看看吧
DIY的空间很大的
包括对上传文件类型定义、文件存放文件夹

------
补充:我传很快,本地就不用说了,不用等待,在网上相比其他一很好
不知道你的接口是怎样配置的
呵呵
欢迎去我博客,我新写了一篇
你照着简历3个文件和一个文件夹,一切OK
我做的是图片上传,你自己去配置下add-sys.asp里面的文件判断函数。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
leletk
2008-09-20 · 超过22用户采纳过TA的回答
知道答主
回答量:78
采纳率:0%
帮助的人:0
展开全部
给你个地址去看一下,可能解决问题。很简单哦!http://leletk.w7.c361.com
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式