又为ajax上传工具重新写了一个服务器端上传程序

作者:fanxiaojie 来源:韦伯花园的魔法师 时间:2009-12-21 14:30:00 

之前写了一个ajax上传工具。但是只是客户端的工具是我写的,服务器端的那个程序,我是修改了一个网上流传的无组件上传类。因为当时我还不懂什么adodb.stream什么的。但是我依然觉得它不够简洁。因为我知道它是可以一次性处理多个文件而且循环处理的。但是ajax上传工具一次只上传一个文件。为了让服务器资源能省的尽量省,我又不懈地研究,重新写了这么一个上传文件。把下面的代码保存为upload.asp吧:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%Option Explicit
Response.Buffer = True
response.charset = "gb2312"
Const uploadfolder="uploadpic"
Const allowExt="jpg,jpeg,gif,png,rar,doc,zip,xls,pdf,swf,avi,mp3,txt,htm,html"
Function BytestoStr(filestr)
 dim skipflag : skipflag = 0
 dim varlen,clow,i
 dim content : content = ""
 if not isnull(filestr) then
  varlen = lenb(filestr)
  for i=1 to varlen
   if skipflag=0 then
    clow = midb(filestr,i,1)
    if ascb(clow) > 127 then
     content = content & chr(ascw(midb(filestr,i+1,1) & clow))
     skipflag = 1
    else
     content = content & chr(ascb(clow))
    end if
   else
    skipflag = 0
   end if
  next
 end if
 BytestoStr = content
End Function
Function getFilepath(ext)
 dim folderpath,fso,filename
 folderpath=server.mappath(uploadfolder)
 set fso=server.createobject("Scripting.FileSystemObject")
 if Fso.FolderExists(folderpath)=false then fso.CreateFolder(folderpath)
 folderpath=folderpath&"\"&date()
 fileinfo=uploadfolder&date()
 if Fso.FolderExists(folderpath)=false then fso.CreateFolder(folderpath)
 filename=cstr(timer()*100)&"."&ext
 getFilepath=folderpath&"\"&filename
 fileinfo=fileinfo&"/"&filename
 set Fso=nothing
End Function
Function CheckExt(fileinfo)
 dim re
 set re=new regexp
 re.global=false
 re.ignorecase=true
 re.pattern="filename=""[^""]+\.(\w{2,4})"""
 if re.test(fileinfo)=false then
  response.write "{""err"":""\u4E0A\u4F20\u6587\u4EF6\u4E0D\u6B63\u786E"",""msg"":""""}"
  CheckExt=false
  set re=nothing
  exit function
 end if
 re.pattern="^[\s\S]+filename=""[^""]+\.(\w{2,4})""[\s\S]+$"
 Ext=re.replace(fileinfo,"$1")
 Ext=lcase(Ext)
 re.pattern="^("&replace(allowExt,",",")|(")&")$"
 if re.test(Ext)=false then
  response.write "{""err"":""\u6587\u4EF6\u7C7B\u578B\u4E0D\u6B63\u786E"",""msg"":""""}"
  CheckExt=false
 else
  CheckExt=true
 end if
 set re=nothing
End Function
If Request.serverVariables("request_method")="POST" then
 if Request.TotalBytes>204800 then
  response.write "{""err"":""\u6587\u4EF6\u592A\u5927\u4E86\2C\u4E0A\u4F20\u6587\u4EF6\u4E0D\u80FD\u8D85\u8FC7200K"",""msg"":""""}"
  response.end
 end if
 dim oStream,oBinary,istart,iseparate,ifinish,ilength,bcrlf,sStream,fileinfo,filepath,ext
 bcrlf=ChrB(13)&ChrB (10)
 set oStream=server.createobject("adodb.stream")
 oStream.Type = 1
 oStream.Mode = 3
 oStream.Open
 oBinary=Request.BinaryRead (Request.TotalBytes)
 oStream.write oBinary
 istart=instrb(oBinary,bcrlf&bcrlf)+3
 iseparate=leftb(oBinary,instrb(oBinary,bcrlf)-1)
 fileinfo=leftb(oBinary,istart-4)
 fileinfo=BytestoStr(fileinfo)
 if CheckExt(fileinfo)=false then
  oStream.close
  set oStream=nothing
  response.end
 end if
 filepath=getFilepath(ext)
 set sStream=server.createobject("adodb.stream")
 sStream.Type = 1
 sStream.Mode = 3
 sStream.Open
 ifinish=instrb(istart,oBinary,iseparate)-3
 ilength=ifinish-istart
 oStream.position=istart
 oStream.Copyto sStream,ilength
 sStream.savetofile filepath,2
 sStream.close
 oStream.close
 set sStream=nothing
 set oStream=nothing
 response.write "{""err"":"""",""msg"":"""&fileinfo&"""}"
End If
%>

这个上传程序,上面有两个参数可以手工修改,一个是上传文件夹,另一个是允许上传的文件格式。它会把上传的文件按日期分存到不同的文件夹里,如果该文件夹不存在,则自动创建。然后文件名是当前时间的厘秒数加扩展名。命名方式其实可以按自己的意愿重新写过的。改那个getfilepath函数就好了。

那个ajax上传组件在这里:

https://www.aspxhome.com/download/2009/12104.htm

标签:上传,ajax,工具,服务器
0
投稿

猜你喜欢

  • 使用cmd命令行窗口操作SqlServer的方法

    2012-07-21 14:24:06
  • Rs.Open参数说明

    2008-05-12 22:43:00
  • asp如何让计数器只对新进用户计数?

    2010-05-13 16:36:00
  • ORACLE常见错误代码的分析与解决(一)

    2010-08-02 13:20:00
  • 将MySQL 5.0下的数据导入到MySQL 3.23中

    2009-01-04 13:02:00
  • asp如何通过表单创建一个Word?

    2010-06-07 20:56:00
  • asp详解session的用法

    2007-09-07 10:22:00
  • 提升设计品质的8个布局方案[译]

    2010-03-18 16:06:00
  • JavaScript程序编码规范[译]

    2009-07-20 17:54:00
  • SQL提高查询效率之Like篇

    2011-10-01 09:36:42
  • asp添加数据实现代码

    2011-02-05 10:42:00
  • asp解决防止表单重复提交的方法

    2007-10-19 18:40:00
  • ASP分页类(支持多风格变换)

    2011-04-08 10:39:00
  • 30个最常用css选择器解析

    2011-06-16 20:36:37
  • MySQL Explain命令用于查看执行效果

    2009-02-27 15:30:00
  • asp获取远程网页的指定内容的实现代码

    2011-02-16 10:41:00
  • 判断 iframe 是否加载完成的完美方法

    2009-09-24 13:35:00
  • asp 随机字符串函数

    2011-04-04 11:01:00
  • MYSQL教程:my.cnf缓存优化

    2009-07-30 08:58:00
  • 关于点击区域

    2009-07-24 13:08:00
  • asp之家 网络编程 m.aspxhome.com