network.download problem

I have a problem with the network.download

Everything seems working until I want to access a file which should be downloaded into the system.DocumentsDirectory … when looking into the Directory there isn’t a file!

Here is my code:

        local thelistis         loadblacklist = function()                          local dothefilechecknow = function()                 local path= system.pathForFile( "blacklistedurls.json", system.DocumentsDirectory )                 local file, errorString = io.open( path, "r" )                               if not file then                     -- Error occurred; output the cause                     print( "Blacklist File error: " .. errorString )                     return false                 else                     -- Read data from file                     local contents = file:read( "\*a" )                     -- Decode JSON data into Lua table                     local t = json.decode( contents )                     -- Close the file handle                     io.close( file )                     -- Return table                     print ("new BLACKLIST now loaded!")                     return t                 end             end             local networkListener = function ( event )                 if ( event.isError ) then                     print( "Network error - download failed: ", event.response )                     -- blendoutplaybuttonandshowmessage()                 elseif ( event.phase == "began" ) then                     print( "Blacklist Progress Phase: began" )                 elseif ( event.phase == "ended" ) then                     -- the files is now ready                     -- the file is now loaded                     print ("Blacklist Progress ended!")                     thelistis=dothefilechecknow() -- we now have a lua table                     -- usage:                     --[[                    print (#thelistis)                     for x=1,#thelistis do                         print (thelistis[x].url)                     end                     --]]                 end             end                           local params = {}             params.progress = true                           network.download(                 "https://WEBSITE/DIRECTORY/blacklistedurls.json",                 "GET",                 networkListener,                 params,                 "blacklistedurls.json",                 system.DocumentsDirectory             )                      end

I always get the – Error occurred; output the cause print( "Blacklist File error: " … errorString )

error which is showing me the local path as errorString in the Simulator output.

What am I missing here?

When you look in the code the whole dothefilechecknow function is only called when the file has finished uploading… so why isn’t it in the directory then?

thelistis=dothefilechecknow()

should probably be:

thelistis=dothefilechecknow(event.response)

Nevermind, you do it differently  :) 

i always use network.request and handle the return file directly in the response, with or without saving it.

it could be a minor OS lag that causes the file not to be available for reading, but that can be tested with a small timer delay.

the code is correct, i just made a json in my server, use your code and all went well.

the only problem i can see is your json file itself. I don’t know if its well formated, or you have the correct permissions to read it.

change "https://WEBSITE/DIRECTORY/blacklistedurls.json"

to

"https://jsonplaceholder.typicode.com/posts"

if the new code works, the problem is in your file.

saying this, your aproach to get blacklistedurls is kinda bad.

you should use network.request a webservice, the response should be a table of json values, you do what you want with those values, you don’t need to copy a file, open it, check if its ok and then read it.

Thank you for your help! Much appreciated!

thelistis=dothefilechecknow()

should probably be:

thelistis=dothefilechecknow(event.response)

Nevermind, you do it differently  :) 

i always use network.request and handle the return file directly in the response, with or without saving it.

it could be a minor OS lag that causes the file not to be available for reading, but that can be tested with a small timer delay.

the code is correct, i just made a json in my server, use your code and all went well.

the only problem i can see is your json file itself. I don’t know if its well formated, or you have the correct permissions to read it.

change "https://WEBSITE/DIRECTORY/blacklistedurls.json"

to

"https://jsonplaceholder.typicode.com/posts"

if the new code works, the problem is in your file.

saying this, your aproach to get blacklistedurls is kinda bad.

you should use network.request a webservice, the response should be a table of json values, you do what you want with those values, you don’t need to copy a file, open it, check if its ok and then read it.

Thank you for your help! Much appreciated!