如何实现ASP无组件文件上传至服务器指定目录?

sunshift 2003-10-09 07:48:34
如何实现ASP无组件文件上传至服务器指定目录?
...全文
62 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
gga7959 2003-10-09
  • 打赏
  • 举报
回复
同意 xiejunhua(trying) 的说法,用化境比较简单而且使用。我就是用那个做的文件上传的。不过现在碰到一个问题了,能传文件上去。可是无发把文件取回来(通过程序来取而不是点链接下载)。
qjrein 2003-10-09
  • 打赏
  • 举报
回复
这是我见过的最短的上传代码
前台
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<BODY topmargin="0" leftmargin="0" bgcolor="papayawhip">
<FORM METHOD=post ACTION="upload1.asp" enctype="multipart/form-data" id=form1 name=form1>
<INPUT TYPE="file" NAME="file" >
<INPUT TYPE="submit" value="上传" id=submit1 name=submit1>
</FORM>
</BODY>
后台
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" type="text/css" href="font.css">
</head>
<body>
<%
dim contentlen
contentlen=request.totalbytes

if contentlen>102400 then
response.write "文件太大,超过100k,不允许上传。请返回"
else

dim content
content=request.binaryread(request.totalbytes)

'二进制相互转换
Function getByteString(StringStr)
getByteString=""
For i=1 to Len(StringStr)
char=Mid(StringStr,i,1)
getByteString=getByteString&chrB(AscB(char))
Next
End Function
Function getString(StringBin)
getString =""
For intCount = 1 to LenB(StringBin)
getString = getString & chr(AscB(MidB(StringBin,intCount,1)))
Next
End Function

dim upbeg,upend,lineone,linetwo,linethree,line1,line2,line3
upbeg=1
upend=instrb(upbeg,content,getbytestring(chr(10)))
lineone=midb(content,upbeg,upend-upbeg)
upbeg=upend+1
line1=lenb(lineone)
upend=instrb(upbeg,content,getbytestring(chr(10)))
linetwo=midb(content,upbeg,upend-upbeg)
upbeg=upend+1
line2=lenb(linetwo)
upend=instrb(upbeg,content,getbytestring(chr(13)))
linethree=midb(content,upbeg,upend-upbeg)
line3=lenb(linethree)

'获得文件名
dim pp,checknametemp,checklen,checkname,filename
pp=instrb(1,linetwo,getbytestring(chr(46)))
checknametemp=rightb(linetwo,line2-pp+1)
checklen=instrb(1,checknametemp,getbytestring(chr(34)))
checkname=getstring(leftb(checknametemp,checklen-1))
filename=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&checkname

'上传文件
dim alllen,upstream,upstreamend,file
alllen=line1+line2+line3+6
set upstream=server.createobject("adodb.stream")
set upstreamend=server.createobject("adodb.stream")
upstream.type=1
upstreamend.type=1
upstream.open
upstreamend.open
upstream.write content
upstream.position=alllen
file=upstream.read(clng(contentlen-alllen-line1-5))
upstreamend.write file
upstreamend.savetofile(server.mappath("news/pic/"&filename)) //该城你需要的目录
upstream.close
upstreamend.close
set upstream=nothing
set upstreamend=nothing

response.write("已上传")
end if
%>
</body>
xiejunhua 2003-10-09
  • 打赏
  • 举报
回复
到google里搜化境无组件上传既可得到想要的东东了.
来自Software Artisans,SA-FileUP组件是一个真正的ActiveX DLL服务器组件,容易集成到ASP网页中。该组件也可在MTS/COM+的环境中上运行。 使用SA-FileUP组件的教程可在http://www.activeserverpages.com/upload/ 上找到 建立SA FileUp 对象的方法: Set oFileUp = Server.CreateObject("SoftArtisans.FileUp") 取出表单所有项的方法: For Each strFormElement In oFileUp.Form 用 oFileUp.Form(strFormElement)就可以引用每个对象,文件也是这样 注意:如果是多选下拉框,则用oFileUp.FormEx(strFormElement) 可以这样来遍历它。 For Each strSubItem In oFileUp.FormEx(strFormElement) Response.Write( strSubItem & "") Next Next ContentType属性: oFileUp.Form(strFormElement).ContentType 可以得到文件的MIME类型 IsEmpty属性 oFileUp.Form(strFormElement).IsEmpty 可以知道用户是不是指定了一个无效的文件 MaxBytes属性 oFileUp.Form(strFormElement).MaxBytes=30000 指定文件的限制,单位为Byte,如果超过它,那么只存储MaxBytes指定的大小。其余舍弃。 ServerName属性 oFileUp.Form(strFormElement).ServerName 可以得到文件保存到服务器的完整路径。 ShortFilename属性 oFileUp.Form(strFormElement).ShortFilename 可以得到客户端的文件名,注意只是文件名,我这里没有用,因为报错。呵呵。 UserFilename属性 oFileUp.Form(strFormElement).UserFileName 可以得到客户端文件的完整路径。可以输出一下给用户看看。 TotalBytes属性 oFileUp.Form(strFormElement).TotalBytes 可以得到文件的大小,单位为Byte SaveInVirtual(路径)方法 oFileUp.Form(strFormElement).SaveInVirtual "upfile/" oFileUp.Form(strFormElement).SaveInVirtual "upfile/aa.abc" 如果只指定了路径,则保留原文件名,否则按指定指定文件名保存 服务器管理员可以禁止掉其他所有方法,但这个一定会留的。 SaveAs (文件名)方法 oFileUp.Form(strFormElement).SaveAs "C:\aa\a.tmp" 如果没有指定路径,只是指定了文件名,那么将用Path属性指定的路径。 Path属性一会介绍。 Save方法 oFileUp.Path="D:\\wwwroot\\abc\\upfile\\" 注意必须是真实路径,可以用Server.MapPath来转换虚拟路径。 oFileUp.Form(strFormElement).Save 不能指定文件名喽。 注意:Path属性必须在提到任何表单项之前,建议放在 Set oFileUp = Server.CreateObject("SoftArtisans.FileUp") 的后面。前提是你用的话。 Delete (文件名,可选) oFileUp.Form(strFormElement).Delete 从服务器上删除文件,如果不指定文件名,则删除当前的文件。 如果指定的话,必须是文件的完整路径。 Flush方法 oFileUp.Flush 当你不想保存任何东西的时候,可以用它来放弃全部的输入流。

28,391

社区成员

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

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