NOT asynchronous network.download needed

Hi All!
I have been playing with the network.download function for a while. It works great, but I have a special request: I don’t want it to be asynchronous !!

Why? Because I have multiple items to download (thounsands of them) and I need them to be downloaded one after another, so that I can check each one while ciclying.

Do you have any idea on how I can do that?

Thank you very much. [import]uid: 86439 topic_id: 24313 reply_id: 324313[/import]

Hi,

maybe you can try a simple http request? It will block further execution of lua code till it is finished:
http://developer.anscamobile.com/reference/index/network

Code sample:

[lua]-- Load the relevant LuaSocket modules
local http = require(“socket.http”)
local ltn12 = require(“ltn12”)

– Create local file for saving data
local path = system.pathForFile( “hello.png”, system.DocumentsDirectory )
myFile = io.open( path, “w+b” )

– Request remote file and save data to local file
http.request{
url = “http://developer.anscamobile.com/demo/hello.png”,
sink = ltn12.sink.file(myFile),
}

– Display local file
testImage = display.newImage(“hello.png”,system.DocumentsDirectory,60,50);[/lua]

Best,
Andreas
[import]uid: 107675 topic_id: 24313 reply_id: 98222[/import]

Thank you very much.
I just need to know how to handle an error if http request fails.
[import]uid: 86439 topic_id: 24313 reply_id: 100248[/import]

Here it is my code, it works, but I am not sure it’s a good code.

local path = system.pathForFile(“download.dat”, system.TemporaryDirectory)
myFile = io.open(path, “w+b”)

local pcallResult, data = pcall(
function()
return http.request {url = “http://www.mysite.com/myfile.txt”, sink = ltn12.sink.file(myFile),}
end
)

if pcallResult then
– success
print(data)
else
– failed
print(pcallResult)
print(data)
end

On my opinion this looks like a try…catch exception handler. Hand made, obvious, but… Still works!

[import]uid: 86439 topic_id: 24313 reply_id: 100261[/import]