http request for files

I am trying to request and display graphics dynamically in my app using the following code

for i=1, #Thumbnail  
do  
 local imagePath = system.pathForFile(i..".jpg", system.TemporaryDirectory)  
 local myFile = io.open(imagePath, "wb")  
  
 http.request{ url = Thumbnail[i], sink = ltn12.sink.file(myFile),}  
  
 display.newImage(i..".jpg", system.TemporaryDirectory, 0, i \* 50);   
end   
  

Thumbnail is a table and each entry contains a valid url to an image. Not sure if the file size is a factor, but the images I am trying to retrieve are all 8k. The issue that I am experiencing is that only the last file in the loop is getting saved properly.

For example if I have total 3 images I want to download and display if I navigate to the system.TemporaryDirectory location in a finder window I can see that my app downloads and creates 3 files called 1.jpg, 2.jpg, 3.jpg however 1 and 2 are corrupt and the size is only 4k where the third file 3.jpg is 8k. Why is that?

The same problem exists if I increase or decrease the number of images I am trying to download, it is only the last image that is created properly.

I’m a beginner to lua and corona and am not sure what I am doing wrong. Can someone please help.

Thanks

Joe [import]uid: 5963 topic_id: 5644 reply_id: 305644[/import]

Some additional information: In the loop if instead of using Thumbnail[i] I specify a url manually “http://www.myserver.com/myimage.jpg” it will successfully create n images. So I’m not sure why using a table breaks it. Perhaps it is a problem with the table? I have printed out the values in the table and they are only “http://www.myserver.com/myimage1.jpg”, “http://www.myserver.com/myimage2.jpg”, “http://www.myserver.com/myimage3.jpg

thanks for any help

Joe [import]uid: 5963 topic_id: 5644 reply_id: 19289[/import]

my guess is you need

[lua]myFile={} myFile[i]=[/lua] etc

i imagine you are overwriting the same file handle so it’s only keeping one

although since myFile is local to the loop it should be a different instance. I’m just wondering if the async nature of http.request it might cause an issue

it’s also possible that calling multiple http.requests might break the previous ones. could you try queueing them? is there a callback event for http.request to say the operation is complete so that you can next request the next one?

try [lua]print(i … “:” … tostring(myFile))[/lua] and see if the 2nd value changes
[import]uid: 6645 topic_id: 5644 reply_id: 19292[/import]

Thanks for the response jmp909

I rewrote the code as you had suggested with no luck

local tblImagePaths = {}  
local tblMyFiles = {}  
  
for i=1, #partThumbnail  
do  
  
tblImagePaths[i] = system.pathForFile(i, system.TemporaryDirectory)  
tblMyFiles[i] = io.open(tblImagePaths[i], "wb")  
  
http.request{ url = partThumbnail[i],  
sink = ltn12.sink.file(tblMyFiles[i]),  
}  
  
end   
  

From what I have read on the forums here a lot of people are complaining that http requests are not async and that your program will block until whatever you have requested is completely fetched, so I’m not sure that is the problem. Since from what I gather the request is not async there is no need for a call back or to queue.

I printed out the myFile as you suggested, and got 1:file (closed), 2:file (closed), etc… for all the files, so I am assuming the writing of the file is complete.

Any other thoughts would be much appreciated as I am still stuck.

Thanks

Joe [import]uid: 5963 topic_id: 5644 reply_id: 19603[/import]

have you tried the async methods?

network.request
network.download

etc

http://developer.anscamobile.com/reference/asynchronous-http [import]uid: 6645 topic_id: 5644 reply_id: 19630[/import]

jmp909 so it turns out that the issue was truly with the table that I was storing the URLs in. even though I iterated through the table and printed out the urls visually something still was not correct. When I created a table and manually inserted the urls it worked. Thanks for your help on this.
[import]uid: 5963 topic_id: 5644 reply_id: 19962[/import]