Tải bản đầy đủ (.pdf) (5 trang)

Giáo Trình Lập Trình Ứng Dụng CSDL Web ASP- P26 pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (204.44 KB, 5 trang )

Gi¸o tr×nh lËp tr×nh øng dông CSDL Web ASP
=== St by hoangly85 ===



126

II. Xây dựng ứng dụng GuestBook :
Thông thýờng các trang Web ðều có sử dụng ỗuestởookự với guest book khi
user duyệt có thể ghi lại các ý kiến cũng nhý các cảm nhận của mình ứ
Trong ví dụ này ta có sử dụng component là ScriptingứỳileSystemỨbjectự ðối
týợng này cho phép ta có rất nhiều thao tác hữu ích ðối với fileự thý mụcứ
Ðể chạy ví dụ này hãy nhập ðoạn code sau và bạn tạo sẵn ử tệp có tên
guestbook.txt trong cùng thý mục với tệp ĩSP này
<%
Const bDeleteEntries = True
Dim bForce
bForce = Request.QueryString("force")
Dim strFile
strFile = Server.MapPath("guestbook.txt")
If Request.Form.Count = 0 Then
%>
<H3>Sign Our Guestbook:</H3>
<FORM ACTION="guestbook.asp" METHOD="post">
<TABLE>
<TR>
<TD ALIGN="right"><B>Name:</B></TD>
<TD><INPUT TYPE="text" NAME="name"
SIZE="15"></INPUT></TD>
</TR>
<TR>


<TD ALIGN="right"><B>Comment:</B></TD>
<TD><INPUT TYPE="text" NAME="comment"
SIZE="35"></INPUT></TD>
</TR>
</TABLE>
<INPUT TYPE="submit" VALUE="Sign Guestbook!"></INPUT>
</FORM>
<BR>
<H3>Today's Comments:</H3>
<! #INCLUDE FILE="guestbook.txt" >
Gi¸o tr×nh lËp tr×nh øng dông CSDL Web ASP
=== St by hoangly85 ===



127

<%
Else
Dim objFSO
Dim objFile
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, 8, True)
objFile.Write "<B>"
objFile.Write Server.HTMLEncode(Request.Form("name"))
objFile.Write ":</B> "
objFile.Write Server.HTMLEncode(Request.Form("comment"))
objFile.Write "<BR>"
objFile.WriteLine ""
objFile.Close

Set objFile = Nothing
Set objFSO = Nothing
%>
<H3>Your comments have been written to the file!</H3>
<A HREF="./guestbook.asp">Back to the guestbook</A>
<%
End If
If bDeleteEntries Then
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strFile)
If DateDiff("d", objFile.DateLastModified, Date()) <> 0 Or bForce <> ""
Then
Set objFile = Nothing
Set objFile = objFSO.CreateTextFile(strFile, True)
objFile.Write "<B>Hello</B> "
objFile.WriteLine "I hope you like this guestbook!<BR>"
objFile.Close
End If
Set objFile = Nothing
Set objFSO = Nothing
Gi¸o tr×nh lËp tr×nh øng dông CSDL Web ASP
=== St by hoangly85 ===



128

End If
%>


II. Xây dựng ứng dụng Directory List
Trong ứng dụng này ta sẽ duyệt một thý mục và hiện lên browser dýới dạng
một bảng biểuự ví dụ nhý sau :

Ðể thực hiện làm ví dụ này hãy tạo trong thý mục chứa tệp ASP này một thý
mục con có tên là images chứa các hình ảnh làm biểu týợng :
STT

Kiểu file Tên tệp icon
1 asp dir_asp
2 dir dir_dir
3 gif, jpg dir_img
4 htm, html dir_htm
5 txt dir_txt
6 các kiểu khác dir_misc
Nhập ðoạn code sau ồ
<%
Function ShowImageForType(strName)
Dim strTemp
strTemp = strName
If strTemp <> "dir" Then
strTemp = LCase(Right(strTemp, Len(strTemp) - InStrRev(strTemp,
".", -1, 1)))
Gi¸o tr×nh lËp tr×nh øng dông CSDL Web ASP
=== St by hoangly85 ===



129


End If
Select Case strTemp
Case "asp"
strTemp = "asp"
Case "dir"
strTemp = "dir"
Case "htm", "html"
strTemp = "htm"
Case "gif", "jpg"
strTemp = "img"
Case "txt"
strTemp = "txt"
Case Else
strTemp = "misc"
End Select
strTemp = "<IMG SRC=""./images/dir_" & strTemp & ".gif"" WIDTH=16
HEIGHT=16 BORDER=0>"
ShowImageForType = strTemp
End Function
%>
<%
Dim strPath
Dim objFSO
Dim objFolder
Dim objItem

strPath = "./dir/" ‘ ðặt ðýờng dẫn thý mục cần xem ở ðây
‘ Sử dụng ðối týợng ỳileSystemỨbject
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath(strPath))

%>
Contents of <B><%= strPath %></B><BR>
<BR>
Gi¸o tr×nh lËp tr×nh øng dông CSDL Web ASP
=== St by hoangly85 ===



130

<TABLE BORDER="5" BORDERCOLOR="green" CELLSPACING="0"
CELLPADDING="2">
<TR BGCOLOR="#006600">
<TD><FONT COLOR="#FFFFFF"><B>File
Name:</B></FONT></TD>
<TD><FONT COLOR="#FFFFFF"><B>File Size
(bytes):</B></FONT></TD>
<TD><FONT COLOR="#FFFFFF"><B>Date
Created:</B></FONT></TD>
<TD><FONT COLOR="#FFFFFF"><B>File
Type:</B></FONT></TD>
</TR>
<%
For Each objItem In objFolder.SubFolders
If InStr(1, objItem, "_vti", 1) = 0 Then
%>
<TR BGCOLOR="#CCFFCC">
<TD ALIGN="left" ><%= ShowImageForType("dir") %>&nbsp;<A
HREF="<%= strPath & objItem.Name %>"><%= objItem.Name %></A></TD>
<TD ALIGN="right"><%= objItem.Size %></TD>

<TD ALIGN="left" ><%= objItem.DateCreated %></TD>
<TD ALIGN="left" ><%= objItem.Type %></TD>
</TR>
<%
End If
Next
For Each objItem In objFolder.Files
%>
<TR BGCOLOR="#CCFFCC">
<TD ALIGN="left" ><%= ShowImageForType(objItem.Name)
%>&nbsp;<A HREF="<%= strPath & objItem.Name %>"><%= objItem.Name
%></A></TD>
<TD ALIGN="right"><%= objItem.Size %></TD>
<TD ALIGN="left" ><%= objItem.DateCreated %></TD>
<TD ALIGN="left" ><%= objItem.Type %></TD>

×