当前位置: 移动技术网 > IT编程>开发语言>Asp > 几个经典的ASP应用

几个经典的ASP应用

2017年12月12日  | 移动技术网IT编程  | 我要评论
1. 下面的代码演示了如何在服务端获取来自客户端浏览器中某一个图片的x,y坐标,注意input控件的类型是image类型。 

 <form>
<input name="imagemap" type="image" src="imagemap.jpg" alt="click anywhere">
</form> 

<%imagemap.x = <%=request("imagemap.x")
imagemap.y = <%=request("imagemap.y")%> 


  2. 利用adodb.stream对象,在ie浏览器中下载服务端上的各类文件。 

  即直接提示用户下载而不是由浏览器打开某些文件。注意,下面的代码拷贝到asp文件中后,不要再添加一些非asp代码在页面中:如html和javascript客户端的代码。


 <%
'--------------------------------------------
response.buffer = true
dim strfilepath, strfilesize, strfilename
const adtypebinary = 1

strfilepath = "文件路径 "
strfilesize = ... 文件大小,可选
strfilename = "文件名"

response.clear

'*******************************************8
' 需要在你的服务器上安装 mdac 2.6 或mdac2.7
'8*******************************************8
set objstream = server.createobject("adodb.stream")
objstream.open
objstream.type = adtypebinary
objstream.loadfromfile strfilepath

strfiletype = lcase(right(strfilename, 4)) '文件扩展名

' 通过文件扩展名判断 content-types
select case strfiletype
case ".asf"
contenttype = "video/x-ms-asf"
case ".avi"
contenttype = "video/avi"
case ".doc"
contenttype = "application/msword"
case ".zip"
contenttype = "application/zip"
case ".xls"
contenttype = "application/vnd.ms-excel"
case ".gif"
contenttype = "../../image/gif"

case ".jpg", "jpeg"
contenttype = "../../image/jpeg"
case ".wav"
contenttype = "audio/wav"
case ".mp3"
contenttype = "audio/mpeg3"
case ".mpg", "mpeg"
contenttype = "video/mpeg"
case ".rtf"
contenttype = "application/rtf"
case ".htm", "html"
contenttype = "text/html"
case ".asp"
contenttype = "text/asp"
case else
'handle all other files
contenttype = "application/octet-stream"
end select

response.addheader "content-disposition", "attachment; filename= strfilename
response.addheader "content-length", strfilesize
response.charset = "utf-8" ' 客户端浏览器的字符集utf-8
response.contenttype = contenttype

response.binarywrite objstream.read
response.flush

objstream.close
set objstream = nothing

%> 


  3.提升asp页面的响应速率 

  在你的asp页面的第一行加入:

 <% enablesessionstate = false %> 

 这会关闭session对象,提升你的服务器响应速率,比较常见的问题是一个html页面包含了两个框架页面(至少有一个是asp页面,并使用了session),这将使得必须等待某一个框架页(当然这个框架页中使用了session)加载完后,另一个框架页面才会显示。

  如果你使用代理访问, 默认情况下,许多代理服务器不会动态缓存asp页面内容,加入下面的代码:

 <%
     response.cachecontrol = "public"
 %> 


  这行代码会将asp页面缓存在代理服务器上,从而加快客户端请求动态页面的响应速率,一些不经常变化的asp页面将直接从代理服务器上取得。 

  4. 要知道浏览器(ie为例)不会解析回车和换行字符,如果你用response.write方法写一行包含了回车和换行字符的字符串到动态页面中,其结果可想而知,你需要做的是: 


 <%
      response.write(replace(body, vbcrlf,"<br>"))
 %> 


  用<br>来代替回车和换行。注意:如果回车和换行字符出现在form中的input/textarea等控件中,可以不必这么做。

  5. 用asp代码写iis日志 


 <%
     response.appendtolog "数据库正在被访问"
 %> 


  执行这段代码后,在你的iis日志中可能会出现下面的字符串:
127.0.0.1, -, 01/01/00, 12:00:34, w3svc1,webserver, 
127.0.0.1, 161342, 485, 228, 200, 0, get, /somefile.asp, 数据库正在被访问 

  注意:由于日志文件中的内容是按逗号分隔,所以写入的日志内容应避免使用逗号。

  6. 如何访问远程计算机上mdb数据库文件 

  如果你用odbc连接(dsn方式或其它方式)到远程计算机的mdb文件,这将产生一个错误:
microsoft ole db provider for odbc drivers error '80004005' 
大致意思是该文件可能被其他用户访问或无足够的权限访问。 

  下面有两种方式,避免这个错误:

    方式a. 使用dao引擎访问


   dim file, conn, rs
  const readonly = false 

  file = "\\server\share\file.mdb"
  set conn = createobject("dao.dbengine.35").workspaces(0).opendatabase(file,,readonly)
  set rs = conn.openrecordset(sql) 


  方式b. ado + jet ole db provider方式


  dim conn, rs
  set conn = createobject("adodb.connection")
  conn.provider = "microsoft.jet.oledb.4.0"
  conn.open "\\server\share\file.mdb"
  set rs = conn.execute(sql) 


   确定在运行asp页面时有足够的访问权限以访问远程计算机上的mdb文件,在访问mdb文件前需要先

  登录到远程计算机,添加下面的代码


     set um = createobject("usermanager.server")
    um.logonuser "帐号", "口令", "域"
...
open database
...
    um.reverttoself

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网