关于ASP编写无组键上传文件的思路和方法.在线等.........

dtysam 2003-05-21 04:54:24
关于ASP编写无组键上传文件的思路和方法.在线等.........


最好能有简单的例子。
...全文
38 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoxingchi 2003-05-22
  • 打赏
  • 举报
回复
收了!
qjrein 2003-05-21
  • 打赏
  • 举报
回复
这是我见过的最短的上传代码
<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
%>
lvyin 2003-05-21
  • 打赏
  • 举报
回复
最近经常有人问到这类问题,在此转贴一下,内容:


1。数据库表结构(Access):
UserID:Text(保存上传文件的用户ID)
FileContentType:Text(用来保存上传文件的类型,eg:"Application/msword",主要用来使用户能正确下载此文件)
FileContent:OLE Object(保存文件数据)

2。HTML文件
muploadfile.htm
<Form name="upload_file" enctype="multipart/form-data" action="muploadfile.asp" method=post>
<input type=hidden name="UserID" value="abc">
<input type=hidden name="FileUploadStart"> '这里用来表示开始文件数据上传
File to send: <BR>
<INPUT TYPE="file" name="file_up" size="30"><br/>
<INPUT TYPE="file" name="file_up" size="30"><br/>
<input type=hidden name="FileUploadEnd"> '这里用来表示文件数据结束
<input type=submit value=Submit>
</Form>

3。ASP文件
muploadfile.asp

<%
Response.Expires=0
Function bin2str(binstr)
Dim varlen,clow,ccc,skipflag

skipflag=0
ccc = ""
If Not IsNull(binstr) Then
varlen=LenB(binstr)
For i=1 To varlen
If skipflag=0 Then
clow = MidB(binstr,i,1)
If AscB(clow) > 127 Then
ccc =ccc & Chr(AscW(MidB(binstr,i+1,1) & clow))
skipflag=1
Else
ccc = ccc & Chr(AscB(clow))
End If
Else
skipflag=0
End If
Next
End If
bin2str = ccc
End Function


varByteCount = Request.TotalBytes
bnCRLF = chrB( 13 ) & chrB( 10 )
binHTTPHeader=Request.BinaryRead(varByteCount)
Divider = LEFTB( binHTTPHeader, INSTRB( binHTTPHeader, bnCRLF ) - 1 )

'开始读非文件域的数据
Do while lenB(binHTTPHeader)>46

binHeaderData = LeftB(binHTTPHeader, INSTRB( binHTTPHeader, bnCRLF & bnCRLF )-1)
strHeaderData=bin2str(binHeaderData)

lngFieldNameStart=Instr(strHeaderData,"name="&chr(34))+Len("name="&chr(34))
lngFieldNameEnd=Instr(lngFieldNameStart,strHeaderData,chr(34))


strFieldName=Mid(strHeaderData,lngFieldNameStart,lngFieldNameEnd-lngFieldNameStart)
strFieldName=Trim(strFieldName)
strFieldName=Replace(strFieldName,vbcrlf,vbnullstring)

'判断文件数据时候开始
If strComp(strFieldName,"FileUploadStart",1)=0 Then
binHTTPHeader=MIDB(binHTTPHeader,INSTRB( DataStart + 1, binHTTPHeader, divider ))
exit do
End if

DataStart = INSTRB( binHTTPHeader, bnCRLF & bnCRLF ) + 4
DataEnd = INSTRB( DataStart + 1, binHTTPHeader, divider ) - DataStart

binFieldValue=MIDB( binHTTPHeader, DataStart, DataEnd )
strFieldValue=bin2str(binFieldValue)
strFieldValue=Trim(strFieldValue)
strFieldValue=Replace(strFieldValue,vbcrlf,vbnullstring)

'非文件上传域变量赋值
execute strFieldName&"="""&strFieldValue&""""


binHTTPHeader=MIDB(binHTTPHeader,INSTRB( DataStart + 1, binHTTPHeader, divider ))

loop

'开始处理文件数据
Do while lenB(binHTTPHeader)>46


binHeaderData = LeftB(binHTTPHeader, INSTRB( binHTTPHeader, bnCRLF & bnCRLF )-1)

strHeaderData=bin2str(binHeaderData)

'读取上传文件的Content-Type
lngFileContentTypeStart=Instr(strHeaderData,"Content-Type:")+Len("Content-Type:")
strFileContentType=Trim(Mid(strHeaderData,lngFileContentTypeStart))
strFileContentType=Replace(strFileContentType,vbCRLF,vbNullString)

'读取上传的文件名
lngFileNameStart=Instr(strHeaderData,"filename="&chr(34))+Len("filename="&chr(34))
lngFileNameEnd=Instr(lngFileNameStart,strHeaderData,chr(34))
strFileName=Mid(strHeaderData,lngFileNameStart,lngFileNameEnd-lngFileNameStart)
strFileName=Trim(strFileName)
strFileName=Replace(strFileName,vbCRLF,vbNullString)

'读取上传文件数据
DataStart = INSTRB( binHTTPHeader, bnCRLF & bnCRLF ) + 4
DataEnd = INSTRB( DataStart + 1, binHTTPHeader, divider ) - DataStart

If strFileName<>"" Then

binFieldValue=MIDB( binHTTPHeader, DataStart, DataEnd )

'将上传的文件写入数据库
set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DSN=abc"

SQL="select * from User_File"
set rs=server.CreateObject("ADODB.Recordset")
rs.Open sql,conn,3,3
rs.addnew
rs("UserID")=UserID
rs("FileContentType")=strFileContentType
rs("FileContent").AppendChunk binFieldValue
rs.update
rs.close
set rs=Nothing
conn.Close
set conn=Nothing

End if

binHTTPHeader=MIDB(binHTTPHeader,INSTRB( DataStart + 1, binHTTPHeader, divider ))

loop
%>

4。下载用户上传的文件
<%
Response.Buffer = true
Response.Clear

UserID=request("UserID")

Set conn=server.createobject("adodb.connection")
set rs=server.createobject("adodb.recordset")
conn.open "DSN=UploadFile"
rs.open "select * from User_File where UserID='"&UserID&"'",conn,3,3
Response.ContentType = rs("FileContentType")

lngOffset=0
conChunkSize=1024
lngPictSize=rs("FileContent").ActualSize
Do While lngOffset < lngPictSize
varChunk = rs("FileContent").GetChunk(conChunkSize)
Response.BinaryWrite varChunk
lngOffset = lngOffset + conChunkSize
If lngOffset > lngPictSize Then Exit Do
Loop

rs.close
set rs=Nothing
conn.close
set conn=nothing
%>

就是这些了,希望此方法对大家能有所帮助。:)
coffee_cn 2003-05-21
  • 打赏
  • 举报
回复
sh.ce.net.cn/web8/tools/up.zip
小鸣歌 2003-05-21
  • 打赏
  • 举报
回复
帮你,帮你顶,
fason 2003-05-21
  • 打赏
  • 举报
回复
搜索,很多例子
http://expert.csdn.net/Expert/topic/885/885309.xml?temp=.7542078

28,391

社区成员

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

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