Claes Gustavsson
Claes Gustavsson

Reputation: 5679

how to read a .txt file from different server in classic asp?

I´m reading a text file from my server as I should with the below, but I wonder how I can read a txt file from a different server? What do I need to do to get it working?

Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("files.txt"), 1)

do while f.AtEndOfStream = false
Response.Write(f.ReadLine)
Response.Write("<br>")
loop

f.Close
Set f=Nothing
Set fs=Nothing

So this is working as it should, but I want to change the files.txt to http://www.somedomain.com/files.txt

Any input appreciated, thanks!

Upvotes: 0

Views: 3316

Answers (2)

Claes , try this and let us know.

<% Option Explicit %>
<%
Const REMOTE_FILE_URL="http://www.somedomain.com/files.txt"

Call ShowRemoteFile

Sub ShowRemoteFile
    Dim objXML, strContents, arrLines
    Dim x
    Set objXML=Server.CreateObject("Microsoft.XMLHTTP")

    'read text file...
    objXML.Open "GET", REMOTE_FILE_URL, False
    objXML.Send
    strContents=objXML.ResponseText
    Set objXML=Nothing

    'split into lines and read line by line...
    arrLines=Split(strContents, VBCrLf)
    For x=0 To UBound(arrLines)
        Response.Write(arrLines(x)&"<br />")
    Next
End Sub
%>

Upvotes: 2

haim770
haim770

Reputation: 49105

Use this function to fetch the text data (taken from here):

Function GetTextFromUrl(url)

  Dim oXMLHTTP
  Dim strStatusTest

  Set oXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.3.0")

  oXMLHTTP.Open "GET", url, False
  oXMLHTTP.Send

  If oXMLHTTP.Status = 200 Then

    GetTextFromUrl = oXMLHTTP.responseText

  End If

End Function

Dim sResult : sResult = GetTextFromUrl("http://www.somedomain.com/files.txt")

Upvotes: 0

Related Questions