How to retrieve a "text" file from a website and display it in a TextArea

I have been searching for 6 months to find a way to retrieve a text file from my website and display it in a bs4 Textarea. When I msgbox the req.status, it returns 0 - which is good … I think… but then I can not display the text. Here is the code I have been trying:

Function btnViewAP_onclick()
  web_address$ = "https://quickplanning.com/tzones.dat"
  filename = web_address$ 
  req=ReadFile(filename)
  If req.status=200 Or req.status=0 Then
    Textarea1.value=Trim(req.responseText)
  Else
    Textarea1.value = req.err
  End If  
End Function

Any suggestions, or does anyone have an example?

I use the following. Note the randomValue which prevents cached data from being returned. Also I am not testing for req.status = 0

randomValue = SysInfo(10)
C = “version.txt?” & randomValue
req=ReadFile(“https://mydomain.com/subbolder/” & C)
If req.status = 200 Then
C = req.responseText

John

Hi John, Thanks so much for your response. I put your code into my project button. I get: req.err = undefined req.status = 0
and of course: req.responseText = undefined
Here is the code I am using:

Function btnViewAP_onclick()
   randomValue = SysInfo(10)
   C = "tzones.dat?" & randomValue    ' line 489
   req=ReadFile("https://QuickPlanning.com/" & C)
    If req.status = 200 Then
        MsgBox (req.status)
        C = req.responseText
        Textarea1.value = C
    Else
        MsgBox("req.err = " & req.err & "    req.status = " & req.status)
        C = req.responseText
        Textarea1.value = C & " - I'm here!"
    End If
End Function

Any Ideas? Can you give it a try?

Paul

Are you running this a web app in Chrome or is it an apk? Is it returning 200? What is the full url of the app trying to read this file as seen in the address bar of Chrome? You might recheck that tzones.dat is on the server in the QuickPlanning folder. Also, use filezilla to download this file ok.

John

John,
I am now running it in Chrome when I test it using the run function in AppStudio. I get the same errors when I deploy it to Voltserver and run it on my iPhone. I will eventually upload as an apa file to Apple.

  • It is not returning 200… it returns req.status = 0
  • the full url is: https://quickplanning.com/tzones.dat
  • I have checked the file is on my server quickplanning, it is there. You can check by just typing the url into your address bar… the file will display.
  • I need to be able to download this file in my program… then I parse it to my particular needs. So filezilla is no benefit. This file is a test to get things working. I will eventually be downloading aviation weather (which are text files also) and parsing them to display in my app.
    Thanks, Paul

As you are not running your app from quick planning.com you are running in to cross origin protection. I know little about this apart from knowing a cross origin setting needs to be changed to allow access to your app.

Readfile will access any files in your deployed app folder without cross origin permissions.

Thanks Graham,
I have run out of ideas. Is there anyone at Admin, that can come up with a work around? Or a way to change the cross origin settings?
Thanks,
Paul

I would also appreciate at step-by-step guide for morons :upside_down_face: ( You know, like for dummies but even simpler) to get a “Hello world” type cross origin access app.

Unfortunately, there is no such thing as a simple one size fits all solution with CORS. Both your app and the server have to be set up properly.

Here’s the thing. Getting a .txt file from the server is one of the many problems that CORS is trying to stop. The fact is, a .txt extension can be a file containing anything. Just because the extension is .txt doesn’t mean the files contents contains actual text.

Inside your page header you have to tell your app what communications are allowed and with whom. Getting a font file from google? You have to specifically enable that google cdn. Want something from your server? You have to enable that access.

From the server side, you also have to tell the app who it’s communicating with. If the app requests data from servera and serverb answers, the app will reject the data. If you request fonts from google and google answers as yahoo, your app would (and should) reject that data.

So, first thing you need to do is create a script on the server to open and feed the .txt file back to the app as JSON. That’s pretty trivial.

$options = Array('status' => "OK", 'version' => $text_from_file);

// return the results to the client
$output = json_encode($options);
echo "$output";       // send it to the browser

The catch is that no matter what format, without the CORS header set correctly, your app will reject everything.

Next, to handle the CORS, a simple trick is to ask the app what domain it’s looking for and feed that back to it. This method is just for testing and NOT for production but it should help you move forward one step. Here’s the basic idea.

httpHeaders.class.php:

<?php
if(!defined('_SOMETAG')) header('Location: https://www.mydomain.com/');

// ini_set('display_errors', 'On');
// error_reporting(E_ALL | E_STRICT);

// this checks the HTTP_ORIGIN header and, depending on the device, it sends back
// the correct header. 
class Headers
{
  public $http_origin = "";
  public $authorized = false;
  public $apiLevel = 1;
  private $DBprefix;

  function __construct()
  {
    $this->getHTTPOrigin(); 
  }
  
  public function sendHeaders($strType)
  {
    header('Access-Control-Allow-Origin: ' .$this->http_origin);   
    error_log("HTTP_ORIGIN: $this->http_origin");

    // error_log("content-type: " .$strType);
    switch($strType)
    {
      case "json":
        header('Content-type: application/json');
        break;

      case "jpeg":
        header('Content-type: image/jpeg'); 
        break;
        
      case "png":
        header('Content-type: image/png');
        break;
      
      case "text":
        header('Content-type: text/plain');
        break;

      case "html":
        header('Content-type: text/html');
        break;
        
      default:
        break;       // do nothing
    }  
  }
  
  // figure out the allowed origin response
  public function getHTTPOrigin()
  {
    // see what the browser is telling us.
    $http_origin = $_SERVER['HTTP_ORIGIN'];
    // error_log("getting HTTP_ORIGIN: " .$http_origin);

    if($http_origin == "")
      // this is probably from... sigh... Android
      $this->http_origin = "https://www.mydomain.com";
    elseif($http_origin == "app://localhost")
      // this is apple/cordova and the new wkwebkit built into 6.1  
      $this->http_origin = $http_origin;
    else
      $this->http_origin = "*";   // just in case something changes and we don't catch it
      
    // error_log("this->http_origin set to: " .$this->http_origin);
  }
}
?>

To test and play with the header class try this:

// figure out what kind of headers we need 
// to send and then send those headers.
$headers = new Headers;
$headers->sendHeaders("json");

If it’s not fundamentally clear, send the headers before sending the JSON.

1 Like

CORS is a major pain for newbies - we all lived thru that. Once you get it set up correctly, it’s not hard to maintain. Can you give us a little insight into what server you are using as that side must be set up correctly as well? It is your server and/or do you have full access? Is it PHP or are you using Amazon or ???

If you search this discussion board for CORS, you’ll find a lot of posts and some examples. There are a lot of great resources on the subject on the internet as well.

Thanks Gary, I think this is way above me! But I really need to solve this. I had written my iPhone app using Mobione and it worked perfectly. For testing I am trying to get the following file off my website. 'https://quickplanning.com/tzones.dat", (You can type that address into your browser and the contents of the file will pop up,)

But eventually I will be requesting weather data off the "https://www.aviationweather.gov" site. (which I can also get raw from there now by typing into my browser and with my old app).

my server is "https://quickplanning.com" and it is PHP . I am the administrator.

I have looked on this site and tried the examples and none are working for me… I’m lost!!! Can I hire you to do a step by step for me?
Paul

Thanks so much, but right now it is way over my head! I don’t even know how to "create a script on the server" and where to put them.

When I finally get this thing going I’ll post a sample app with the solution.

Who’s your web host? Apache and PHP based?

Gotta learn that server side but you can’t just read a text file from a server (any server).

Thanks Phil,
My web host is GoDaddy and it as PHP.
Paul

I‘ve not used godaddy but on every LAMP server I’ve seen, it doesn’t matter where you put the .php files. I’ll cook up a little test/example for you in an hour or two.

Thank you very much…

paul.zip (1.4 KB)

Here ya go!
Step 1: create a folder one level below the text file (since it’s in your root)
Step 2: unzip the attached file and use an ftp program to upload the file to the folder you created in step 1
Step 3: make sure the folder permissions are set to 755 and the file permissions are 644.
Step 4: call https://quickplanning.com/folder/serGetFile.php from your app.
Step 5: when your app gets a response from the server text status field will either be OK or FAILED and the text field will either be your data or some message.

I didn’t debug this on my server but if you run call it from the browser using the url in step 4, you should see the text file (or some error messages). LOL

Phil:

Just curious: I’m using req=ReadFile(filename) without issue on my server (bluehost.com). The files to be downloaded are either at or below the app folder. I can also use php as you have noted. Is there some reason you know of that GoDaddy doesn’t work with Ajax or ReadFile?

Thanks, John

Godaddy should work fine with Ajax, the problem will be CORS.