Microsoft KB Archive/272656

= How to Create a File Viewer By Using ASP =

Article ID: 272656

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft Internet Information Services 6.0
 * Microsoft Internet Information Server 4.0
 * Microsoft Internet Information Services 5.0

-



This article was previously published under Q272656



We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 6.0 running on Microsoft Windows Server 2003. IIS 6.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



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 AspEnableParentPaths MetaBase Property Should Be Set To False

276548 ASP Error 0131 When Browsing to Database Results Region ASP Page



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.  Open Notepad on a Web server that is running Microsoft Internet Information Server version 4.0 or Internet Information Services version 5.0.  Run the following ASP code: <% @Language=&quot;VBScript&quot; %> <%   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(&quot;LOGON_USER&quot;)))) = 0 Then Response.Status = &quot;401 Access Denied&quot; Response.End End If %> File Viewer

File Viewer

&quot; method=&quot;POST&quot;> &quot;> 

<% ' was it a POST request? If UCase(Request.ServerVariables(&quot;HTTP_METHOD&quot;)) = &quot;POST&quot; Then

' create object for file I/O Set objFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;) ' open the specified file Set objFILE = objFSO.OpenTextFile(Request.Form(&quot;FILE&quot;)) ' output message if an error has occured If Err.Number <> 0 Then

Response.Write &quot;Error trying open the file &quot;&quot;&quot; & _ Request.Form(&quot;FILE&quot;) & &quot;&quot;&quot;&quot; & vbCrLf & _ &quot;Error Number = &quot; & Err.Number & vbCrLf & _ &quot;Error Description = &quot; & Err.Description & vbCrLf

' otherwise show the file Else

' show the page start Response.Write &quot;- START OF PAGE -&quot; & vbCrLf ' loop through the page contents While Not objFILE.AtEndOfStream Response.Write Server.HTMLEncode(objFILE.ReadLine) & vbCrLf Wend

' show the page end Response.Write &quot;- END OF PAGE -&quot; & vbCrLf

' close the specified file objFILE.Close ' discard the file I/O object Set objFSO = Nothing

End If

End If %>  Save the page as &quot;Viewer.asp&quot;in the root folder of your Web site. 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.</ol>