文章編號: 272656 - 上次校閱: 2006年11月23日 - 版次: 1.2

如何使用 ASP 建立檔案檢視器

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。
本文曾發行於 CHT272656

在此頁中

全部展開 | 全部摺疊

結論

本文說明如何使用 Microsoft Active Server Pages (ASP) 技術,以 Scripting.FileSystemObject 來建立一般的檔案檢視器。

重要注意事項



如果您不當使用本文中的程式碼,此程式碼可用來檢視位於網站外的資料夾。 如需避免發生此問題的其他資訊,請按一下下面文件編號,檢視 Microsoft Knowledge Base 中的下面文件:
184717? (http://support.microsoft.com/kb/184717/EN-US/ ) AspEnableParentPaths MetaBase Property Should Be Set To False
276548? (http://support.microsoft.com/kb/276548/EN-US/ ) ASP Error 0131 When Browsing to Database Results Region ASP Page

其他相關資訊

一般免責聲明



Microsoft 提供之程式設計範例僅供說明之用,Microsoft 並不提供任何明示或默示之擔保,其中包括但不限於適售性以及適合某特定用途之默示責任擔保。本文假設您熟悉文中展示的程式設計語言以及建立程序和程序除錯使用的工具。Microsoft 支援專家可以幫助您解說特殊程序的功能,但是不會修改這些範例以提供新增的功能或建構符合您需求的程序。若您的程式設計經驗有限,可以連絡 Microsoft Certified Partner,或 Microsoft 付費顧問專線 (800) 936-5200。如需有關 Microsoft Certified Partner 的詳細資訊,請參閱下面全球資訊網網頁:
https://partner.microsoft.com/global/30000104 (https://partner.microsoft.com/global/30000104)
如需得知 Microsoft 有提供哪些支援方案,請參閱下面全球資訊網網頁:
http://support.microsoft.com/directory/overview.asp (http://support.microsoft.com/?scid=http%3a%2f%2fsupport.microsoft.com%2fdirectory%2foverview.asp)

頁面檢視器程式碼範例



請使用下列步驟來建立頁面檢視器 ASP 頁面。

注意:您必須啟用 [基本/使用純文字傳送密碼]、[整合式/NTLM] 或 [摘要式] 驗證,才能使用此範例。
  1. 在執行 Microsoft Internet Information Server 4.0 版或 Internet Information Services 5.0 版的 Web 伺服器上開啟「記事本」。
  2. 執行下面 ASP 程式碼:
    <% @Language="VBScript" %>
    <%  
    On Error Resume Next ' don't worry about errors
    ' 
    turn on buffering
    Response.Buffer = True
    ' make sure that client is authenticated
    If 
    Len(Trim(CStr(Request.ServerVariables("LOGON_USER")))) = 0 Then
    Response.Status 
    = "401 Access Denied"
    Response.End
    End If
    %>
    <html>
    <head>
    <title>
    File Viewer
    </title>
    </head>
    <body>
    <h2>File 
    Viewer</h2>
    <!-- show a form to allow users to specify a file -->
    <form action="<%=Request.ServerVariables("URL")%>
    " 
    method="POST">
    <input type="text" name="FILE" value="<%=Request.Form("FILE")%>">
    <input 
    type="submit" value="View File">
    </form>
    *.pre
    <%
    ' was it a POST request?
    If UCase(Request.ServerVariables("HTTP_METHOD")) 
    = "POST" Then
    ' create object for file I/O
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    ' 
    open the specified file
    Set objFILE = objFSO.OpenTextFile(Request.Form("FILE"))
     
       
    ' output message if an error has occured
    If Err.Number <>
    0 Then
    Response.Write 
    "Error trying open the file """ & _
    Request.Form("FILE") & """" & vbCrLf & _
    "Error 
    Number = " & Err.Number & vbCrLf & _
    "Error Description = " & Err.Description & 
    vbCrLf
    ' otherwise show the file
    Else
    ' show the page start
    Response.Write "----- 
    START OF PAGE -----" & vbCrLf
        
    ' loop through the page contents
    While Not objFILE.AtEndOfStream
    Response.Write 
    Server.HTMLEncode(objFILE.ReadLine) & vbCrLf
    Wend
    ' show the page end
    Response.Write 
    "----- END OF PAGE -----" & vbCrLf
    ' close the specified file
    objFILE.Close
    ' discard 
    the file I/O object
    Set objFSO = Nothing
    End If
    End If
    %>
    *.pre
    </body>
    </html>
    
    
  3. 將此頁面存檔為「Viewer.asp」,放在您的網站的根目錄下。
  4. 透過 HTTP 瀏覽至此頁面,然後輸入您想要檢視的目錄路徑。此資料夾的內容應該會顯示出來。

參考



若需有關此主題的其他資訊,請按一下下面的文件編號,檢視「Microsoft 知識庫」中的文件:
218606? (http://support.microsoft.com/kb/218606/EN-US/ ) HOWTO:使用 ASP 與 Scripting.FileSystemObject 來建立動態目錄頁
224364? (http://support.microsoft.com/kb/224364/EN-US/ ) Creating a Directory Browsing Page Using ASP
201133? (http://support.microsoft.com/kb/201133/EN-US/ ) Scripting Change Occurs When Upgrading from IIS 3.0

?考

本文件是根據 Microsoft Knowledgebase 文件編號 Q272656 翻譯的. 若要參考原始英文文件內容, 請至以下網址:

http://support.microsoft.com/support/kb/articles/Q272/6/56.asp (http://support.microsoft.com/kb/272656/en-us?ln=en-us&sd=gn&fr=0)

這篇文章中的資訊適用於:
  • Microsoft Internet Information Server 4.0
  • Microsoft Internet Information Services 5.0
關鍵字:?
kbhowto kbiis KB272656
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。