network.download - Can't seem to download a text file from a website

I apparently don’t understand how network.download works. I have a website with a text file uploaded to it. I would like to read the data within the text file from my app to make app updates as I change the text file data. I’m using network.download to get the text file but instead of downloading the text file I’m downloading the html source code from the website. 

Here’s my code. Anybody know why I would be getting the html and not the actual file?

[lua]

local function networkListener( event )

    if ( event.isError ) then

        print( “Network error - download failed” )

    elseif ( event.phase == “began” ) then

        print( “Progress Phase: began” )

    elseif ( event.phase == “ended” ) then

        print( “Displaying response file” )

        local path = system.pathForFile( “temp.txt”, system.TemporaryDirectory )

        local file = io.open( path, “r” )

        for line in file:lines() do

          print( line )

        end

       io.close( file )

       file = nil

    end

end

local params = {}

params.progress = true

network.download(

    “http://brya1659.wix.com/info/PedPTEx.txt”,

    “GET”,

    networkListener,

    params,

    “temp.txt”,

    system.TemporaryDirectory

)

[/lua]

Thanks for any help,

Guy

Hi Guy,

I think this is to do with how your website is set up, rather than an issue with your Corona code.  When I copy and paste the link http://brya1659.wix.com/info/PedPTEx.txtinto my browser, I do get an HTML page (which is exactly what Corona is downloading).  According to your webpage, the download link to your text file is actually at http://www.anyfile.io/wix/download?id=4289efca19b101eeda37db54110254df.  Try downloading that instead - something like:

network.download(     "http://www.anyfile.io/wix/download?id=4289efca19b101eeda37db54110254df",     "GET",    ..  etc.)

I’d be worried using that link in production though - it looks like it might change every time you upload a new version of the file…

Hi Happymongoose,

Thanks for the help! I tried the download link according to the webpage and it worked. However, you are correct. If I upload new data then it needs a new link, so this will not work. I had another idea, so I created a “hidden blog” and put the data in it. Sure enough when I tried to download the “blog’s location” I got the html along with the blog contents. So, I created a parser routine to extract the data I needed and it worked perfect. I changed the data and it worked! So, for now this is a viable set up for what I need.

Thanks again,

Guy

Hi Guy,

I think this is to do with how your website is set up, rather than an issue with your Corona code.  When I copy and paste the link http://brya1659.wix.com/info/PedPTEx.txtinto my browser, I do get an HTML page (which is exactly what Corona is downloading).  According to your webpage, the download link to your text file is actually at http://www.anyfile.io/wix/download?id=4289efca19b101eeda37db54110254df.  Try downloading that instead - something like:

network.download(     "http://www.anyfile.io/wix/download?id=4289efca19b101eeda37db54110254df",     "GET",    ..  etc.)

I’d be worried using that link in production though - it looks like it might change every time you upload a new version of the file…

Hi Happymongoose,

Thanks for the help! I tried the download link according to the webpage and it worked. However, you are correct. If I upload new data then it needs a new link, so this will not work. I had another idea, so I created a “hidden blog” and put the data in it. Sure enough when I tried to download the “blog’s location” I got the html along with the blog contents. So, I created a parser routine to extract the data I needed and it worked perfect. I changed the data and it worked! So, for now this is a viable set up for what I need.

Thanks again,

Guy