学习笔记-ASP第一步-内部对象和常见组件2-- 常见组件

 6. 加强ASP的功能 —— ASP的常用组件简介

   ASP可以通过ActiveX组件(由VB、VC++、Delphi等任何编程语言编写的ActiveX库)扩充ASP的功能(ASP本身附带了如ADO、FSO等组件,使用其他组件需要下载安装包并在服务器上安装注册)。

 

  6.1 使用 FileSystemObject 组件读取和编辑文件

    FileSystemObject组件 一般简称为FSO组件,是一个对文件系统进行读取、写入、复制、删除等操作的组件,使用FSO组件,可以方便的编辑服务器上的文件。

    6.1.1 使用FSO读取文本文件的页面read.asp

      <%

        Const ForReading = 1  ' 以只读方式打开文件。

        Const ForWriting = 2  ' 以写方式打开文件。

        Const ForAppending = 8  ' 打开文件并从文件末尾开始写。

        

        Dim oFSO

        Set oFSO = Server.CreateObject("Scripting.FileSystemObject")

        

        Dim oFile

        Dim sFileName

        Dim sFileContent

        

        sFileName = "test.txt"  ' 要编辑的文件名称  // 先建个test.txt文档(内容随意)以便操作

        Set oFile = oFSO.OpenTextFile(Server.MapPath(sFileName),ForReading)  ' 打开文件对象并赋值给变量oFile

        

        sFileContent = oFile.ReadAll()

        oFile.Close

        Set oFile = Nothing

        Set oFSO = Nothing

      %>

       <from action="save.asp" method="post">

        <textarea rows="10" cols="50" name="FileContent"><%= sFileContent%></textarea>

        <input type="submit" value="保存">

      </form>

    6.1.2 使用FSO保存文件的页面 save.asp

      <%

        Const ForReading = 1

        Const ForWriting = 2

        Const ForAppending =8

      

        Dim sFileContent

        sFileContent = Trim(Request.Form("FileContent"))

 

        Dim oFSO

        Set oFSO = Server.CreatObject("Scripting.FileSystemObject")

        

        Dim oFile

        Dim sFileName

 

        sFileName = "test.txt"

        Set oFile = oFSO.OpenTextFile(Server.MapPath(sFileName),ForWriting)

 

        oFile.Write(sFileContent)

        oFile.Close

        Set oFile = Nothing

        Set oFSO = Nothing

        Response.Write("编写完成")

      %>

    6.1.3 关于FSO

      还提供了对于驱动器和文件夹两种文件系统对象的访问功能。

方法 说明
① DriveExists(drivespec) 如果再drivespec中指定的驱动器存在,则返回True;否则返回False。drivespec参数可以试一个驱动器字母,

或者是文件、文件夹的完整绝对路径。

② GetDrive(drivespec) 返回drivespec指定的驱动器所对应的Drive对象。drivespec可以包含冒号、路径分隔符或者是网络共享名,

即:"c"、"c:"、"c:\"、\\machine\sharename"。

③ GetDriveName(drivespec) 用字符串返回drivespec指定的驱动器名称。drivespec参数必须是文件或文件夹的绝对路径,

或者仅仅是驱动器字母。例如:"c:" 或 "c"。

④ CopyFolder(source,destination,overwrite) 从指定源文件夹source(可以包含通配符)中复制一个或多个文件夹到指定的目标文件夹

destination,包含了源文件夹中的所有文件。如果source包含通配符或destination末尾

是路径分隔符("\")那么认为destination是要防止源文件夹的考本的文件夹。否则的话,

认为destination是要穿件的新文件夹的路径名。如果destination文件夹已经存在

且overwrite参数设置的为False,将产生错误,缺省的wverwrite参数是True。

⑤ CreateFolder(floldername) 创建一个路径名为foldername的文件夹。如果foldername已经存在将产生错误。
⑥ DeleteFolder(folderspec,force) 删除由folderspec指定的一个或多个文件夹(可以再路径的最后部分包含通配符)及文件夹中的所有内容。

如果可选的force参数设置为true,那么即使文件夹包含的文件具有只读属性,也将删除文件夹。

缺省的force参数是Fales。

⑦ FolderExists(folderspec) 如果folderspec指定的文件夹存在则返回True;否则返回False。

Folderspec参数可以包含文件夹的绝对或相对路径,或者仅仅是当前文件夹中看到的文件夹名。

⑧ GetAbsolutePathNmae(pathspec) 返回明确指定文件夹的路径,其中要考虑到当前文件夹的路径。

例如:如果当前文件夹是"c:docs\sales\",而pathspec是"jan",

返回的字符是"c:\docs\sales\jan"。通配符、".."、"\\"路径操作符都是可以接受的。

⑨ GetFolder(folderspec) 返回folderspec指定的文件夹对应的Folder对象。Folderspec可以试文件夹的相对的或绝对的路径。
⑩ GetParentFolderName(pathspec) 返回pathspec文件或文件夹的上一级文件夹。不见眼该文件夹是否存在。
⑪ GetSpecialfolder(folderspec) 返回一个与特定的Windows文件夹相对应的Folder对象。

参数folderspec的允许值是WindowsFolder(0)、SystemFolder(1)、TemporaryFolder(2)。

MoveFolder(source,destination) 将source指定的一个或多个文件夹移动到destination指定的文件夹。在source里可以包含通配符,

但在destination中不行。如果source包含通配符或destination末尾是路径分隔符("\"),

则认为destination是要放置源文件夹的文件夹,否则认为它是一个新文件夹的完整路径和名字。

如果目的文件夹destination已经存在则产生错误。

 

  6.2 使用Jmail 组件发送邮件

    Jmail 组件是由Dimac的公司编写的一款在线发送邮件的组件。可从官网:http://www.dimac.net下载安装包。

    安装后,在安装目录下可以找到Jmail 的手册,手册里详细讲解了Jmail 组件的方法、属性等。

    6.2.1 实例应用

      guestbook.htm(表单页面)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

    <html xmlns=http://www.w3.org.1999.xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html;charset=gb2312"/>

    <title>Mail</title>

    </head>

 

    <body>

    <form id="form1" name="form1" method="post" action="mail.asp">

      <label>标题:

      <input type="text" name="Subject" id="Subject" />

      </label>

      <p>

        <label>内容:

        <textarea name="content" id="content" cols="45" rows="5"></textarea>

        </label>

      </p>

      <p> <input type="submit" name="button" id="button" value="发送" /> </p>

    </form>

    </body>

    </html>

    

      mail.asp 页面

    <%

      On Error Resume Next

      Dim strRecipient,Subject,Body

      strRecipient="webmaster@website.com"  '假设webmaster@website.com 是网站管理人员的邮箱

      Subject=Trim(Request.Form("Subject"))

      Body=Trim(Request.Form("content"))

      Dim jmail

      Set jmail=Server.CreateObject("JMAIL.Message")  '建立发送邮件的对象

      jmail.silent = true               ' 屏蔽例外错误

      jmail.logging = true              ' 启用邮件日志

      jmail.Charset = "GB2312"           ' 邮件的文字编码为国标

      jmail.ContentType = "text/html"         ' 邮件的格式为HTML格式

      jmail.Addrecipient strRecipient           ' 收件人的地址

      jmail.Form = mailsending@sohu.com      ' 发件人的E-MAIL地址

      jmail.MailServerUserName = "rosy"        ' 登录邮件服务器所需的用户名

      jmail.MailServerPassword = "rosy"                    ' 登录邮件服务器所需的密码

      jmail.Subject = Subject             ' 邮件的标题

      jmail.Body = Body               ' 邮件内容

      jmail.Priority = 1                ' 邮件的优先级。1为最快,5为最慢,3为默认。

      jmail.Send("smtp.sohu.com")          ' 执行邮件发送(通过邮件服务器地址)

      jmail.Close()                  ' 关闭对象

      Set jmail = Nothing

 

      If Err Then

        Err.Clear

        Response.Write("出现错误:" & Err.Description)

      Else

        Response.Write("邮件发送成功!")

      End If

    %>

     

    注意:

      MailServerUserNmae:发件人使用的SMTP服务器上的用户名

      MailServerPassword:发件人使用的SMTP服务器上的密码

      send方法附加了一个参数,即smtp服务器的地址。

--------------------------------------------------------------------------------------------------------------

    6.2.2 封装发送邮件函数

    <%

      ' EendMailByJmail

      ' input:  strRecipient - 收件人

      '      Subject - 邮件主题

      '      Body - 邮件内容

      Function SendMailByJmail(strRecipient, Subject, Body)

        Dim jmail

        Set jmail = Server.CreateObject("JMAIL.Message")  '建立发送邮件的对象

        jmail.silent = true               ' 屏蔽例外错误

        jmail.logging = true              ' 启用邮件日志

        jmail.Charset = "GB2312"           ' 邮件的文字编码为国标

        jmail.ContentType = "text/html"         ' 邮件的格式为HTML格式

        jmail.Addrecipient strRecipient           ' 收件人的地址

        jmail.Form = mailsending@sohu.com      ' 发件人的E-MAIL地址

        jmail.MailServerUserName = "rosy"        ' 登录邮件服务器所需的用户名

        jmail.MailServerPassword = "rosy"                    ' 登录邮件服务器所需的密码

        jmail.Subject = Subject             ' 邮件的标题

        jmail.Body = Body               ' 邮件内容

        jmail.Priority = 1                ' 邮件的优先级。1为最快,5为最慢,3为默认。

        jmail.Send("smtp.sohu.com")          ' 执行邮件发送(通过邮件服务器地址)

        jmail.Close()                  ' 关闭对象

        Set jmail = Nothing 

      End Function

    %>

 

 

 

  6.3 使用AspJpeg 组件编辑图像 —— 穿件一个图片的缩略图

    AspJpeg是由Persits Software,Inc 编写的可在ASP/ASP.NET中对图片文件进行编辑处理的组件。

    AspJpeg可以使用很少的代码在你的ASP/ASP.NET应用程序上动态的创建高质量的缩略图图像。

    AspJpeg支持的图像格式有:JPEG,GIF,BMP,TIFF,PNG。

    AspJpeg还可以做到:生成水印图片、图片合并、图片切割、数据库支持、安全吗技术等。

    可在http://www.aspjpeg.com官网下载AspJpeg组件的最新版本,安装后重启IIS即可使用。

    (AspJpeg是一个商业软件,试用1个月)

    6.3.1 实例应用

    <%

      Set Jpeg = Server.CreateObject("Persits.Jpeg")  ' 建立AspJpeg对象的实例Jpeg

      Path = Server.MapPath("../images/apple.jpg")    ' 得到被编辑图片的绝对路径

      Jpeg.Open Path                  ' 打开被编辑的图片 apple.jpg

      ' 缩略图部分

      Jpeg.Width = Jepg.OriginalWidth / 2      ' 宽减半 

      Jpeg.Height = Jpeg.OriginalHeight / 2    ' 高减半

      ' 绘制文字部分    

      Jpeg.Canvas.Font.Color = &HFFFFFF     ' 设定颜色为白色    

      Jpeg.Canvas.Font.Family = "宋体"       ' 设定字体为宋体

      Jpeg.Canvas.Font.Bold = False        ' 设定不加粗

      Jpeg.Canvas.Font.Size = 20           ' 设定字号为20

      Jpeg.Canvas.Font.Quality = 10        ' 设定文字质量为10(可选0~10, 10最高)

      Jpeg.Canvas.Print 10,10,"Text By AspJpeg"  ' 在图片上写文字

      ' 将编辑完毕的图片保存为apple_small.jpg

      Jpeg.Save Server.MapPath("apple_small.jpg")

    %>

      Original Image:<br>

      <img src="../images/apple.jpg"><p>

      

      Thumbnail(50%):<br>

      <img src="apple_small.jpg">

    

 

 

posted @ 2014-06-17 15:21  被盗的小偷  阅读(176)  评论(0)    收藏  举报