登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

面包会有的

... ...

 
 
 

日志

 
 

张:一个完整的上传  

2008-06-23 10:18:25|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |


效果:此上传除了弹出文件选择对话框外,不再弹出其它,而且会将图片地址自动填入输入框,并且自动还原上传表单.非常漂亮,是本人调试正确的.
添加的页面:用iframe调入文件选择的输入框
<form name=myform method=post action='admin_admanage.asp'>
<tr id=flagsetting6<%If isEdit Then If Rs("flag")=5  Then Response.Write " style=""display:none"""%>>
 <td class=TableRow1 align=right><strong>上传文件:</strong></td>
 <td class=TableRow1> <iframe name="image" frameborder=0 width=100% height=25 scrolling=no src="../UpLoadFiles/upfilehtm.asp?formname=myform&amp;editname=ImageUrl"></iframe>
</td>
</tr>
</form>

以下文件统一放在UploadFiles文件夹下,文件清单:upfile.asp+upfilehtm.asp+upload.inc+pic文件夹

-----------upfile.asp的完整代码------------------------------------------------------

<%
dim FilePath,FileMaxSize,FileType,fileweb,nameset,pathset
FilePath="/UpLoadFiles/pic/"
FileMaxSize="25000"
FileType=".jpeg.jpg.gif.rar.zip.swf"
fileweb="/UpLoadFiles/pic/"
nameset =2
pathset =1

%>
<!--#include FILE="upload.inc"-->
<html>
<head>
<title>文件上传系统 </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<table width="100%" border="0">
<tr>
<td>
<div align="center">
<%
dim upload,file,formName,iCount,UpFilePath,TypeFlag,fname,FileNameStr,Msg
set upload=new upload_5xSoft ''建立上传对象

if Pathset > 0 then   ''得到上传目录
 UpFilePath = upload.form("filepath")
else
 UpFilePath = FilePath
 ''在目录后加(/)
 if right(UpFilePath,1)<>"/" then UpFilePath=UpFilePath&"/"
end if

iCount=0
for each formName in upload.file ''列出所有上传了的文件
 set file=upload.file(formName)  ''生成一个文件对象
 if file.FileSize>0 then         ''如果 FileSize > 0 说明有文件数据
  if file.FileSize<FileMaxSize then ''如果未超过文件大小限制

   if FileType<>"" then '如果限制了上传文件类型
    if Instr(FileType,GetExtendName(file.FileName)) then
     TypeFlag = 1        '文件为允许的类型
    else
     TypeFlag = 0  '文件为不允许的类型
      Response.write "不支持您所上传的文件类型:"
     Response.write GetExtendName(file.FileName)
     Response.write "<br>"
    end if
   else
    TypeFlag = 1   '没有限制上传文件类型
 
   end if
 
   if TypeFlag = 1 then

    if nameset = 1 then
     fname = vfname & iCount & "." & GetExtendName(file.FileName)
    elseif nameset =2 then
     fname = file.FileName
    elseif nameset = 3 then
     fname = vfname & iCount & file.FileName
    end if
    randomize
    fname=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&(int(900*rnd)+100)&"."&GetExtendName(file.FileName)
    file.SaveAs Server.mappath(UpFilePath&fname)   ''保存文件
    'response.write file.FilePath&file.FileName&" ("&file.FileSize&") => 上传文件成功! <br>"
    session("imgaddress")=fileweb&fname
    iCount=iCount+1
    ' fileStr = fileStr & "<img src='pic/addon.gif'><a href='"& UpFilePath&file.FileName&"' target='_blank'>查看上传的文件:<font color='red'>" & file.FileName &"</font> ("& file.FileSize &" kb)</a><br>"
    FileNameStr = UpFilePath&fname
    'response.write "<a href="&Filepath&fname&">点击这里查看上传图片的效果</a><br>"
    'response.write "你的文件路径是<font color='ff0000'>("&fileweb&fname&")</font> <a href="&Filepath&fname&">也可点击这里查看上传图片的效果</a><br>"
    response.write "<script>parent.document."&upload.form("FormName")&"."&upload.form("EditName")&".value='"&fileweb&fname&"'</script>"
    response.write "<script language=vbscript>history.go(-1)</script>"
   end if

  else
   response.write "文件大小超出限制,您最多可以上传 "& FileMaxSize &"个字节的文件数据"
   exit for
  end if
  end if
  set file=nothing
next
set upload=nothing  ''删除此对象

if iCount>0 then
 'response.write "<br>成功地上传了"&iCount&" 个文件!"
 response.write "<br>"&Msg&"<br><center> [<a href=""javascript:history.back();""><<返回</a>]</center>"
 ' response.end
else
 response.write "<br>上传文件失败,请向管理员反应!"
 response.write "<br>"&Msg&"<br><center> [<a href=""javascript:history.back();""><<返回</a>]  [<a href=""javascript:window.close();"">关闭窗口</a>]</center>"
 ' response.end
end if

sub HtmEnd(Msg)
 set upload=nothing
end sub


function GetExtendName(FileName)
 dim ExtName
 ExtName = LCase(FileName)
 ExtName = right(ExtName,3)
 ExtName = right(ExtName,3-Instr(ExtName,"."))
 GetExtendName = ExtName
end function
%>
</div>
</td>
</tr>
</table>
</body>
</html>

----------------upfilehtm.asp的完整代码-----------------------------------------------
<%
dim formname,editname
formName=request("formName")   '回传到上页面编辑框所在Form的Name
EditName=request("EditName")   '回传到上页面编辑框的Name
%>
<html>
<head>
<title>上传照片</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="../admin/images/style.css" type="text/css" media="all" />
<script language=javascript>
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(".swf") == -1) && (document.all[sId].value.indexOf(".rar") == -1) && (document.all[sId].value.indexOf(".zip") == -1)){
    alert("请选择gif、jpg、jpeg的图象文件");
    event.returnValue = false;
    }
}
</script>
</head>
<body topmargin="0">

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<form name="form1" method="post" action="upfile.asp" enctype="multipart/form-data" onSubmit="checkImage('file1')">
<input type="hidden" name="act" value="upload">
<tr align="center" valign="middle">
<td class=TableRow2 height="60" align="left" id="upid">
<input type="hidden" class="tx" value="1" name="upcount">
<input type="hidden" name="filepath" value="pic/">
<input type="hidden" name="EditName" value="<%=EditName%>">
<input type="hidden" name="FormName" value="<%=formName%>">
<input type="file" name="file1" style="width:400" class="tx1" value=""  size="50">
</td>
<td class=TableRow2>
<input type="submit" name="Submit" value="提 交" class="bt">
<input type="reset" name="Submit2" value="清 除" class="bt">
</td>
</tr>
</form>
</table>

</body>
</html>

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










  评论这张
 
阅读(809)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018