欢迎光临
我们一直在努力

无组件上传图片之文件采用方案-ASP教程,组件开发

建站超值云服务器,限时71元/月

首先,图片在页面中能查找选择。设计表单页面index.asp和上传选择页upload.asp,upload.asp在index.asp中以iframe包含。

其次,所选图片应能上传到某文件夹。建立一文件夹uploadimg

最后,传上去的图片应如何引用?很显然,采用ubb立即显示。upload.asp的指向对象upfile.asp具有写入ubb标签的功能。

图片上传采用稻香老农的无组件上传。所以upload.inc文件必不可少。

1,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>

[ctrl+a 全部选择 提示:你可先修改部分代码,再按运行]

2,表单页面index.asp。注意包含的上传选择页upload.asp

<form name="form_name" method="post" action="add.asp">

<textarea cols="100" name="cn_content" rows="18" width="100%"></textarea>

</form>

<iframe border="0" frameborder="0" noresize scrolling="no" width="100%" src="upload.asp"></iframe>

3,上传选择页upload.asp 注意: enctype="multipart/form-data"

<form name="form" method="post" action="upfile.asp" enctype="multipart/form-data">

<input type="hidden" name="filepath" value="uploadimg">

<input type="hidden" name="act" value="upload">

<input type="file" name="file1" size=40>

<input type="submit" class=button name="submit" value="上传图片" onclick="parent.document.forms[0].submit.disabled=true">类型:gif,jpg,限制:100k

</form>

4,最后一 个文件 upfile.asp 主要作用:生成图片名,并将图片上传,同样也要将ubb标签写入index.asp中的textarea中。

(拷贝以下文本框的所有代码)

<!–#include file="upload.inc"–>

<html>

<head>

<title>文件上传</title>

</head>

<body>

<script>

parent.document.forms[0].submit.disabled=false;

</script>

<%

dim upload,file,formname,formpath,icount,filename,fileext

set upload=new upload_5xsoft 建立上传对象

formpath=upload.form("filepath")

在目录后加(/)

if right(formpath,1)<>"/" then formpath=formpath&"/"

response.write "<body>"

icount=0

for each formname in upload.file 列出所有上传了的文件

set file=upload.file(formname) 生成一个文件对象

if file.filesize<100 then

response.write "请选择你要上传的文件 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"

response.end

end if

if file.filesize>100*1000 then

response.write "文件大小超过了限制100k [ <a href=# onclick=history.go(-1)>重新上传</a> ]"

response.end

end if

fileext=lcase(right(file.filename,4))

uploadsuc=false

forum_upload="gif,jpg,png"

forumupload=split(forum_upload,",")

for i=0 to ubound(forumupload)

if fileext="."&trim(forumupload(i)) then

uploadsuc=true

exit for

else

uploadsuc=false

end if

next

if uploadsuc=false then

response.write "文件格式不正确 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"

response.end

end if

randomize

rannum=int(90000*rnd)+10000

filename=formpath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&rannum&fileext

if file.filesize>0 then 如果 filesize > 0 说明有文件数据

file.saveas server.mappath(filename) 保存文件

for i=0 to ubound(forumupload)

if fileext="."&trim(forumupload(i)) then

response.write "<script>parent.form_name.cn_content.value+=[img]"&filename&"[/img]</script>"

exit for

end if

next

icount=icount+1

end if

set file=nothing

next

set upload=nothing 删除此对象

htmend icount&" 个文件上传结束!"

sub htmend(msg)

set upload=nothing

response.write "上传成功 [ <a href=# onclick=history.go(-1)>继续上传</a>]"

response.end

end sub

%>

</body>

</html>

[ctrl+a 全部选择 提示:你可先修改部分代码,再按运行]

当然,保持图片的文件夹uploadimg不能少

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 无组件上传图片之文件采用方案-ASP教程,组件开发
分享到: 更多 (0)

相关推荐

  • 暂无文章