标题:无组件上传图片的代码,
只看楼主
fly406
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2008-5-9
结帖率:100%
 问题点数:0 回复次数:7 
无组件上传图片的代码,
<%
  Response.Buffer = True            '建立缓存
  Server.ScriptTimeOut=9999999        '定义超期时间
  On Error Resume Next
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="Content-Language" content="zh-cn" />
<style type="text/css">
<!--
body,input {font-size:12px;}
body {
    background-color: #9999FF;
}
-->
</style>
<title>ASP文件上传工具</title>
</head>
<body id="body">
<%
  ExtName = "jpg,gif,png,txt,rar,zip,doc"                   '允许的上传文件扩展名
  SavePath = "upload"                                         '文件保存路径
  If Right(SavePath,1)<>"/" Then SavePath=SavePath&"/"        '在目录后加(/)
  CheckAndCreateFolder(SavePath)

  UpLoadAll_a = Request.TotalBytes                             '取得客户端全部内容
  If(UpLoadAll_a>0) Then
    Set UploadStream_c = Server.CreateObject("ADODB.Stream")'建立对象
    UploadStream_c.Type = 1
    UploadStream_c.Open
    UploadStream_c.Write Request.BinaryRead(UpLoadAll_a)
    UploadStream_c.Position = 0

    FormDataAll_d = UploadStream_c.Read
    CrLf_e = chrB(13)&chrB(10)
    FormStart_f = InStrB(FormDataAll_d,CrLf_e)
    FormEnd_g = InStrB(FormStart_f+1,FormDataAll_d,CrLf_e)

    Set FormStream_h = Server.Createobject("ADODB.Stream")
    FormStream_h.Type = 1
    FormStream_h.Open
    UploadStream_c.Position = FormStart_f + 1
    UploadStream_c.CopyTo FormStream_h,FormEnd_g-FormStart_f-3
    FormStream_h.Position = 0
    FormStream_h.Type = 2
    FormStream_h.CharSet = "GB2312"
    FormStreamText_i = FormStream_h.Readtext
    FormStream_h.Close

    FileName_j = Mid(FormStreamText_i,InstrRev(FormStreamText_i,"\")+1,FormEnd_g)

    If(CheckFileExt(FileName_j,ExtName)) Then
      SaveFile = Server.MapPath(SavePath & FileName_j)

      If Err Then
        Response.Write "文件上传: <span style=""color:red;"">文件上传出错!</span> <a href=""" & Request.ServerVariables("URL") &""">重新上传文件</a><br />"
        Err.Clear
      Else
        SaveFile = CheckFileExists(SaveFile)

        k=Instrb(FormDataAll_d,CrLf_e&CrLf_e)+4
        l=Instrb(k+1,FormDataAll_d,leftB(FormDataAll_d,FormStart_f-1))-k-2
        FormStream_h.Type=1
        FormStream_h.Open
        UploadStream_c.Position=k-1
        UploadStream_c.CopyTo FormStream_h,l
        FormStream_h.SaveToFile SaveFile,2
        
        SaveFileName = Mid(SaveFile,InstrRev(SaveFile,"\")+1)
        Response.write "文件上传: <span style=""color:red;"">" & SaveFileName & " </span>文件上传成功! <a href=""" & Request.ServerVariables("URL") &""">继续上传文件</a><br />"
      End If
    Else
      Response.write "文件上传: <span style=""color:red;"">文件格式不正确!</span> <a href=""" & Request.ServerVariables("URL") &""">重新上传文件</a><br />"
    End If

  Else
%>
<script language="Javascript">
<!--
function ValidInput()
{
   
if(document.upform.upfile.value=="")
  {
    alert("请选择上传文件!")
    document.upform.upfile.focus()
    return false
  }
  return true
}
// -->
</script>
<form action='<%= Request.ServerVariables("URL") %>' method='post' name="upform" onsubmit="return ValidInput()"  enctype="multipart/form-data">
文件上传:
<input type='file' name='upfile' size="40"> <input type='submit' value="上传">
</form>
<%
  End if
  Set FormStream_h = Nothing
  UploadStream.Close
  Set UploadStream = Nothing
%>
</body>
</html>
<%
  '判断文件类型是否合格
  Function CheckFileExt(FileName,ExtName) '文件名,允许上传文件类型
    FileType = ExtName
    FileType = Split(FileType,",")
    For i = 0 To Ubound(FileType)
      If LCase(Right(FileName,3)) = LCase(FileType(i)) then
      CheckFileExt = True
      Exit Function
      Else
      CheckFileExt = False
      End if
    Next
  End Function

  '检查上传文件夹是否存在,不存在则创建文件夹
  Function CheckAndCreateFolder(FolderName)
    fldr = Server.Mappath(FolderName)
    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not fso.FolderExists(fldr) Then
      fso.CreateFolder(fldr)
    End If
    Set fso = Nothing
  End Function

'检查文件是否存在,重命名存在文件
Function CheckFileExists(FileName)
  Set fso=Server.CreateObject("Scripting.FileSystemObject")
  If fso.FileExists(SaveFile) Then
    i=1
    msg=True
    Do While msg
      CheckFileExists = Replace(SaveFile,Right(SaveFile,4),"_" & i & Right(SaveFile,4))
      If not fso.FileExists(CheckFileExists) Then
        msg=False
      End If
      i=i+1
    Loop
  Else
    CheckFileExists = FileName
  End If
  Set fso=Nothing
End Function
%>
搜索更多相关主题的帖子: 代码 组件 
2008-06-20 03:11
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
得分:0 
9999999  这个是多少时间?
2008-06-26 13:49
anlige
Rank: 3Rank: 3
等 级:新手上路
威 望:7
帖 子:401
专家分:0
注 册:2006-11-3
得分:0 
很长时间,秒为单位,就是让他不会产生超时错误~

http://www.
zhanghuiguoanlige@
2008-06-29 01:28
BOSS级菜鸟
该用户已被删除
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2008-06-29 14:16
dyuter
Rank: 1
等 级:新手上路
帖 子:59
专家分:0
注 册:2006-6-30
得分:0 
试过了,同意楼上说的,要是发个空的,页面也会转向,其它没问题,谢谢LZ提供源代码。
2008-07-19 02:12
lele2007
Rank: 5Rank: 5
来 自:广东省深圳
等 级:职业侠客
威 望:6
帖 子:1028
专家分:305
注 册:2007-9-4
得分:0 
看不懂是什么意思?

学习编程www.
2008-07-19 12:25
ruanjian2110
Rank: 3Rank: 3
来 自:武汉
等 级:论坛游民
威 望:7
帖 子:486
专家分:50
注 册:2008-5-25
得分:0 
不错,学习学习。。。。

武汉信控科技有限公司:http://www. 超越技术,本身就是一个美好技术。。。。。。
2008-07-19 19:45
lausong2010
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-3-18
得分:0 
如果上传的目录是ftp服务器上指定的目录,此方式能实现吗?
2011-03-18 16:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-220129-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.280802 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved