How best to determine successful network.download

I’m downloading 5 filenames. Not all 5 will produce images but filenames get created.

So far it’s good.

I want to find out which are valid images.
I can do this by size as the empty ones are quite small compared to a real image.

How can I check file size? or is there a better way to do this?
All I want is to know what names produced images so that I can show buttons or thumbs only for those.

[import]uid: 6547 topic_id: 21870 reply_id: 321870[/import]

Two possible things to suggest.
One… Try to load it as a display.newimage
If it isn’t valid, your object will be nil

Or try opening it as a text file… A missing file will result in a text file holding the text of an http 404 file not found error.
[import]uid: 108660 topic_id: 21870 reply_id: 86939[/import]

Thanks for good suggestions but the first one doesn’t work due to bug in the SDK and since I have to stick with pre 4.3 requirement I will probably never see a fix.

Second one doesn’t work because a filename is always created. A file not found error doesn’t come back as an error so even if the file doesn’t exist on the server, no error, just an empty file created.

That’s why I was thinking of using file size as the “trigger”. The ‘empty’ file is about 2k while a real image is somewhat larger.

Haven’t been able to figure out how to get that done yet though.

EDIT: Have it ‘roughed’ out using file:seek(). Now for the cleanup/assembly. [import]uid: 6547 topic_id: 21870 reply_id: 86946[/import]

what are you trying to do?

you can always read the file as binary for x amount of bytes -

are you trying to determine if a successful download occurred of a file or an image file?

if in binary form, you can read the header of a file (png,jpg) and look for the PNG and or JPG signature and or the width and height of the image and get the real size. then determine by file : seek if file size matches

c
[import]uid: 24 topic_id: 21870 reply_id: 86966[/import]

I’m just trying to determine if an image was really downloaded by network.download.

Then based on that I’ll show a button or maybe even a thumbnail to actually open the image.

I’ve got it pinned down now, I think.

  
 function checkImage(\_index)  
 local path = system.pathForFile( myImages[\_index], system.TemporaryDirectory )  
 local file = io.open( path, "r" )  
 if file then -- nil if no file found  
 local current = file:seek() -- get current position  
 local size = file:seek("end") -- get file size  
 file:seek("set", current) -- restore position  
 print("size=",size)  
 io.close( file )  
 return size  
 else  
 print ("no good image")  
 return 0  
 end  
 end  

Still kinda rough but getting there, I think/hope :slight_smile:

[import]uid: 6547 topic_id: 21870 reply_id: 86981[/import]

Yeah, but 2K is not an empty file, and unless you know for certain yours are bigger, a downloaded image could be 2K

When Ive tried this in the past, what I find the ‘empty file’ contains is not spaces but something like

Error: 404 File not found
…somethings

Just open it in a text editor and have a look see.

>>first one doesn’t work due to bug in the SDK<<

Unless 4.3 is different in a way that I don’t know about, I have to disagree.
here is actual code from my own app and it does work, I’m using it right now:

 pic = nil  
 bWasPresent = false  
 local pic = display.newImage("chart".. n..".png" , system.CachesDirectory)   
 if pic then  
 --debugprint ("the picture was there!",pic.width,pic.height)  
  
 if pic.width \> 0 and pic.height \> 0 then  
 bWasPresent = true  
 end   
 else  
 --debugprint ("the picture was missing or invalid!")  
 end  

[import]uid: 108660 topic_id: 21870 reply_id: 87051[/import]

Correct re not empty. It’s the error (404) page but since any of my images are all known to be greater I was going to try that.

Have since changed to checking content for the 404 text.

Going to try your scheme tomorrow as I’m not happy with performance.

Just to be clear re the 4.3 reference. It’s iOS version I’m referring to, since I have to support 3 and 3s I have to stick with SDK Build 7.0.4 as anything after needs iOS 4.3 or better and that’s not available for the older iPhone/iPod.

EDIT: Not totally implemented yet, but looking much better than my earlier attempts. Big Thank You. [import]uid: 6547 topic_id: 21870 reply_id: 87053[/import]