0

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!

Claes Gustavsson
  • 5,509
  • 11
  • 50
  • 86

2 Answers2

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
%>
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

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")
Community
  • 1
  • 1
haim770
  • 48,394
  • 7
  • 105
  • 133