当前位置: 首页 > 学习 > 电脑学习 > 网站开发 > ASP > ASP技巧 > 正文

很好的上传!无组件!文件上传!包含其他文本字段!文件自动更名!简单易用!

http://www.zk168.com.cn  招考学习网 2006-4-30 14:17:39
-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--
其中部分代码根据化境代码改编!!
upload.htm
<html>

<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
<link rel="stylesheet" href="../public/css.css">
</head>
<script language="javascript">
<!--
function checkform1()
  {

  if (form1.names.value=="")
    {
    alert("请输入名称!")
    form1.names.focus()
    return false
    }
  if (form1.pic.value=="")
    {
    alert("请选择图片!")
    form1.pic.focus()
    return false
    }
  return true
  }

//-->
</script>
<body>

<table border="0" width="100%">
<form name="form1" enctype="multipart/form-data" action="upload.asp" method="post" onsubmit="return checkform1()">
  <tr>
    <td width="100%" colspan="2">
      <p align="center"><b>上传示例</b></td>
  </tr>
  
  <tr>
    <td width="40%" align="right">名称:</td>
    <td width="60%"><input type="text" name="names" size="30" maxlength="20" width="40%" align="right">图样:</td>
    <td width="60%"><input type="file" name="pic" size="30" maxlength="20" width="40%" align="right"> </td>
    <td width="60%"><input  type="submit" value="   o k   " width="100%" align="center" colspan="2">
    图象预览(选择图片后,点击页面任意处即可进行预览)<br>
    <img id="picview" border="1">
    </td>
    
  </tr>
  </form>
</table>

</body>

</html>


-----upload.asp
<!--#include file="savefile.asp"-->
<%
Set objStream = Server.CreateObject("ADODB.Stream")
  
  objstream.mode=3
  objStream.Type = 1
  objStream.Open
  
  objstream.write Request.BinaryRead(Request.TotalBytes)
  
  names= getvalue("names",true,"")
  
  pic=getvalue("pic",false,"upimg/")
  

objstream.close
response.write("names:"+names)
response.write ("文件已上传!("+pic+")")
%>


-----savefile.asp
<%
  Dim stream1,stream2,istart,iend,filename
  istart=1
  vbEnter=Chr(13)&Chr(10)

function getvalue(fstr,foro,paths)'fstr为接收的名称,foro布尔false为文件上传,true 为普通字段,path为上传文件存放路径
  if foro then
    getvalue=""
    istart=instring(istart,fstr)

    istart=istart+len(fstr)+5
    iend=instring(istart,vbenter+"-----------------------------")
    if istart>5+len(fstr) then
    getvalue=substring(istart,iend-istart)
   
    else
    getvalue=""
    end if
    else
    istart=instring(istart,fstr)
    istart=istart+len(fstr)+13
    iend=instring(istart,vbenter)-1
    
    filename=substring(istart,iend-istart)
    filename=getfilename(filename)
  
    istart=instring(iend,vbenter+vbenter)+3
    iend=instring(istart,vbenter+"-----------------------------")
    filestart=istart
    filesize=iend-istart-1
    objstream.position=filestart
    Set sf = Server.CreateObject("ADODB.Stream")
    sf.Mode=3
    sf.Type=1
    sf.Open
    objstream.copyto sf,FileSize
    
    if filename<>"" then
    Set rf = Server.CreateObject("Scripting.FileSystemObject")
    i=0
    fn=filename
    while rf.FileExists(server.mappath(paths+fn))

      fn=cstr(i)+filename
      i=i+1
      wend
    filename=fn
    sf.SaveToFile server.mappath(paths+filename),2
    end if
    getvalue=filename
    end if
  
end function

Private function GetFileName(FullPath)
  If FullPath <> "" Then
   GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)
  Else
   GetFileName = ""
  End If
End  function


Function inString(theStart,varStr)
dim i,j,bt,theLen,str
InString=0
Str=toByte(varStr)
theLen=LenB(Str)
for i=theStart to objStream.Size-theLen
   if i>objstream.size then exit Function
   
   objstream.Position=i-1
   if AscB(objstream.Read(1))=AscB(midB(Str,1)) then
    InString=i
    for j=2 to theLen
      if objstream.EOS then  
        inString=0
        Exit for
      end if
      if AscB(objstream.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


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

Function subString(theStart,theLen)
dim i,c,stemp
objStream.Position=theStart-1
stemp=""
for i=1 to theLen
   if objStream.EOS then Exit for
   c=ascB(objStream.Read(1))
   If c > 127 Then
    if objStream.EOS then Exit for
    stemp=stemp&Chr(AscW(ChrB(AscB(objStream.Read(1)))&ChrB(c)))
    i=i+1
   else
    stemp=stemp&Chr(c)
   End If
Next
subString=stemp
End function
%>


呵呵!希望大家喜欢!
-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--
最新入库:
 
·10. 结束电话
·9 电话故障
·8. 分机、占线
·7. 打错电话
·6. 长途电话
·5. 转告消息
·4. 留 言
·3. 等候与回电
·2. 没找到人
·1. 打电话 接电话
相关内容:
 
网友点评:
 
会员名称:
密码:匿名 ·注册·忘记密码?
评论内容:
(最多300个字符)
  查看评论
友情提醒:
 1.库中的资料大都来自互联网、网友上传、各类书籍,在录入的过程中难免会出现错误,恳请网
 友来信指正!
 2.如果网友在本库中未能找到所需要的材料,请登陆到我们的论坛《招考学习网》版块!
 3.考友想加入招考学习网的编辑部,请发信到XueXiWang#Gmail.com(#改为@)附带个人简历
 4.如需转载请注明出处及作者,谢谢合作!
 5.如果您有更好的建议或意见请EMAIL:XueXiWang#Gmail.com (#改为@)
 6.凡标题中有注有“[NO]”字样均不含答案且答案整理中.
 7.如本库中转载文章涉及版权等问题,请相关网站或作者在两周内发邮件通知(EMAIL:  XueXiWang#Gmail.com (#改为@))我们,我们接到通知后立即删除该文章及链接!
你问我答 更多>>