Microsoft Knowledge Base Email Alertz

This article describes how to use Microsoft Active Server Pages (ASP) technologies to create a generic file viewer by using the

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 272656 - Last Review: July 7, 2008 - Revision: 7.1

How to Create a File Viewer By Using ASP

This article was previously published under Q272656
We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 7.0 running on Microsoft Windows Server 2008. IIS 7.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site:
http://www.microsoft.com/technet/security/prodtech/IIS.mspx (http://www.microsoft.com/technet/security/prodtech/IIS.mspx)
For more information about IIS 7.0, visit the following Microsoft Web site:
http://www.iis.net/default.aspx?tabid=1 (http://www.iis.net/default.aspx?tabid=1)

On This Page

SUMMARY

This article describes how to use Microsoft Active Server Pages (ASP) technologies to create a generic file viewer by using the Scripting.FileSystemObject.

Important Note

If you use the code in this article improperly, the code can be used to view folders outside of a Web site. For additional information on preventing this, click the article numbers below to view the articles in the Microsoft Knowledge Base:
184717  (http://kbalertz.com/Feedback.aspx?kbNumber=184717/EN-US/ ) AspEnableParentPaths MetaBase Property Should Be Set To False
276548  (http://kbalertz.com/Feedback.aspx?kbNumber=276548/EN-US/ ) ASP Error 0131 When Browsing to Database Results Region ASP Page

MORE INFORMATION

General Disclaimer

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

Page Viewer Example Code

Use the following steps to create the page viewer ASP page.

NOTE: You must have Basic/Clear Text, Integrated/NTLM, or Digest authentication enabled to use this example.
  1. Open Notepad on a Web server that is running Microsoft Internet Information Server version 4.0 or Internet Information Services version 5.0.
  2. Run the following ASP code:
    <% @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. Save the page as "Viewer.asp"in the root folder of your Web site.
  4. Browse to the page through HTTP, and then enter the path of the directory that you want to view. The folder's contents should display.

REFERENCES

For additional information on this topic, click the article numbers below to view the articles in the Microsoft Knowledge Base:
218606  (http://kbalertz.com/Feedback.aspx?kbNumber=218606/EN-US/ ) HOWTO: ASP and Scripting FileSystemObject to Create Dynamic TOC
224364  (http://kbalertz.com/Feedback.aspx?kbNumber=224364/EN-US/ ) Creating a Directory Browsing Page Using ASP
201133  (http://kbalertz.com/Feedback.aspx?kbNumber=201133/EN-US/ ) Scripting Change Occurs When Upgrading from IIS 3.0

APPLIES TO
  • Microsoft Internet Information Services 6.0
  • Microsoft Internet Information Server 4.0
  • Microsoft Internet Information Services 5.0
Keywords: 
kbhowto KB272656
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate