Downloading Multiple Images via http.request

Hi, I am having trouble getting codea to download multiple images by downloading one at a time. I want it to download image1 and once that has finished, it will start downloading image2 and so on. Here is the part of the code I am talking about: (It is not my whole project)

    
        if BootTime >= 20 and               
           Download==0 and
           asset==1 then
        --Achievements
        http.request('https://www.dropbox.com/s/0tqm3wd11894xr5/achievements.png?dl=1',gotImage1)
    end
    
    if asset==2 then
        --AutoUp
        print("yay")
        http.request("https://www.dropbox.com/s/pe1b43fpirac1sb/autoup.png?dl=1",gotImage2)
    end
    if asset==3 then
        --Background01
        http.request("https://www.dropbox.com/s/pxw2dg5ockpxjwf/background01.png?dl=1",gotImage3)
    end
    if asset==4 then
        --BakerMan
        http.request("https://www.dropbox.com/s/1sk23um8ph7gqd9/bakerman.png?dl=1",gotImage4)
    end
    if asset==5 then
        --Bread
        http.request("https://www.dropbox.com/s/vf91kvuan58tj5q/bread.png?dl=1",gotImage5)
    end
    if asset==6 then
        --BreadHole
        http.request("https://www.dropbox.com/s/gikvny643u9p4fc/breadhole.png?dl=1",gotImage6)
    end
    if asset==7 then
        --BreadSeeds
        http.request("https://www.dropbox.com/s/2x04z2vte0myjtl/breadseeds.png?dl=1",gotImage7)
    end
    if asset==8 then
        --DesktopBack
        http.request("https://www.dropbox.com/s/rd3ln4b7djh3x89/desktopback.png?dl=1",gotImage8)
    end
    if asset==9 then
        --DesktopBlue
        http.request("https://www.dropbox.com/s/nv52qnzo0fjmn9w/desktopblue.png?dl=1",gotImage9)
    end
    if asset==10 then
        --DesktopClose
        http.request("https://www.dropbox.com/s/bfgrhtyc7ip1i71/desktopclose.png?dl=1",gotImage10)
    end
    if asset==11 then
        --DesktopGames
        http.request("https://www.dropbox.com/s/48qkigr0yy0glla/desktopgames.png?dl=1",gotImage11)
    end
    if asset==12 then
        --DesktopStartBlue
        http.request("https://www.dropbox.com/s/34uw7g3d954oatw/desktopstartblue.png?dl=1",gotImage12)
    end
    if asset==13 then
        --Doughinator
        http.request("https://www.dropbox.com/s/dydqeo7pc95mzd7/doughinator.png?dl=1",gotImage13)
    end
    if asset==14 then
        --Feedback
        http.request("https://www.dropbox.com/s/3r66z9nvo528jga/feedback.png?dl=1",gotImage14)
    end
    if asset==15 then
        --Grandma
        http.request("https://www.dropbox.com/s/lc6q7g2a0bybzi3/grandma.png?dl=1",gotImage15)
    end
    if asset==16 then
        --LogOnBackground
        http.request("https://www.dropbox.com/s/ocs3sg10syvsbxi/logonbackground.png?dl=1",gotImage16)
    end
    if asset==17 then
        --LuckyDough
        http.request("https://www.dropbox.com/s/gr3erjhqkkarrbv/luckydough.png?dl=1",gotImage17)
    end
    if asset==18 then
        --MutantYeast
        http.request("https://www.dropbox.com/s/3y72pbhqn8ad7by/mutantyeast.png?dl=1",gotImage18)
    end
    if asset==19 then
        --Options
        http.request("https://www.dropbox.com/s/yuokk9vnq2tpdhw/options.png?dl=1",gotImage19)
    end
    if asset==20 then
        --OvenUpG
        http.request("https://www.dropbox.com/s/7ph26qmnr10rf4h/ovenupg.png?dl=1",gotImage20)
    end
    if asset==21 then
        --Slime
        http.request("https://www.dropbox.com/s/zh82vp57alxfm7f/slime.png?dl=1",gotImage21)
    end
    if asset==22 then
        --SlimeBackground
        http.request("https://www.dropbox.com/s/8rrml30q2kx4sf5/slimebackground.png?dl=1",gotImage22)
    end
    if asset==23 then
        --SlimeEgg
        http.request("https://www.dropbox.com/s/4mdpuqc1h3rqog7/slimeegg.png?dl=1",gotImage23)
    end
    if asset==24 then
        --SlimeInTank
        http.request("https://www.dropbox.com/s/jx6t5yu82u98vq2/slimeintank.png?dl=1",gotImage24)
    end
    if asset==25 then
        --SlimeInTankEmpty
        http.request("https://www.dropbox.com/s/2pg3gsono1et4yf/slimeintankempty.png?dl=1",gotImage25)
    end
    if asset==26 then
        --Statistics
        http.request("https://www.dropbox.com/s/uv1yy9t0b2qi7pc/statistics.png?dl=1",gotImage26)
    end
    if asset==27 then
        --TimeMachine
        http.request("https://www.dropbox.com/s/uz8we1oewvnb14e/timemachine.png?dl=1",gotImage27)
    end
    if asset==28 then
        --Toaster
        http.request("https://www.dropbox.com/s/68mpi6rstbyjlo5/toaster.png?dl=1",gotImage28)
    end
    if asset==29 then
        --UnluckyDough
        http.request("https://www.dropbox.com/s/xhsdp8g1db6y45m/unluckydough.png?dl=1",gotImage29)
    end
                
function gotImage1(image,status,header)
        
        Asset1=image
        saveImage("Project:Achievements",Asset1)
        asset=2
        
    end
    
function gotImage2(image,status,header)
        
        print("all good")
        Asset2=image
        saveImage("Project:AutoUp",Asset2)
        asset=3
        print("cool")
        
    end
    
function gotImage3(image,status,header)
        
        print("awesome")
        Asset3=image
        saveImage("Project:Background01",Asset3)
        asset=4
        
    end
    
function gotImage4(image,status,header)
        
        Asset4=image
        saveImage("Project:BakerMan",Asset4)
        asset=5
        
    end
    
function gotImage5(image,status,header)
        
        Asset5=image
        saveImage("Project:Bread",Asset5)
        asset=6
        
    end
    
function gotImage6(image,status,header)
        
        Asset6=image
        saveImage("Project:BreadHole",Asset6)
        asset=7
        
    end
    
function gotImage7(image,status,header)
        
        Asset7=image
        saveImage("Project:BreadSeeds",Asset7)
        asset=8
        
    end
    
function gotImage8(image,status,header)
        
        Asset8=image
        saveImage("Project:DesktopBack",Asset8)
        asset=9
        
    end
    
function gotImage9(image,status,header)
        
        Asset9=image
        saveImage("Project:DesktopBlue",Asset9)
        asset=10
        
    end
    
function gotImage10(image,status,header)
        
        Asset10=image
        saveImage("Project:DesktopClose",Asset10)
        asset=11
        
    end
    
function gotImage11(image,status,header)
        
        Asset11=image
        saveImage("Project:DesktopGames",Asset11)
        asset=12
        
    end
    
function gotImage12(image,status,header)
        
        Asset12=image
        saveImage("Project:DesktopStartBlue",Asset12)
        asset=13
        
    end
    
function gotImage13(image,status,header)
        
        Asset13=image
        saveImage("Project:Doughinator",Asset13)
        asset=14
        
    end
    
function gotImage14(image,status,header)
        
        Asset14=image
        saveImage("Project:Feedback",Asset14)
        asset=15
        
    end
    
function gotImage15(image,status,header)
        
        Asset15=image
        saveImage("Project:Grandma",Asset15)
        asset=16
        
    end
    
function gotImage16(image,status,header)
        
        Asset16=image
        saveImage("Project:LogOnBackground",Asset16)
        asset=17
        
    end
    
function gotImage17(image,status,header)
        
        Asset17=image
        saveImage("Project:LuckyDough",Asset17)
        asset=18
        
    end
    
function gotImage18(image,status,header)
        
        Asset18=image
        saveImage("Project:MutantYeast",Asset18)
        asset=19
        
    end
    
function gotImage19(image,status,header)
        
        Asset19=image
        saveImage("Project:Options",Asset19)
        asset=20
        
    end
    
function gotImage20(image,status,header)
        
        Asset20=image
        saveImage("Project:OvenUpG",Asset20)
        asset=21
    
    end
    
function gotImage21(image,status,header)
        
        Asset21=image
        saveImage("Project:Slime",Asset21)
        asset=22
        
    end
    
function gotImage22(image,status,header)
        
        Asset22=image
        saveImage("Project:SlimeBackground",Asset22)
        asset=23
        
    end
    
function gotImage23(image,status,header)
        
        Asset23=image
        saveImage("Project:SlimeEgg",Asset23)
        asset=24
    
    end
    
function gotImage24(image,status,header)
        
        Asset24=image
        saveImage("Project:SlimeInTank",Asset24)
        asset=25
        
    end
    
function gotImage25(image,status,header)
        
        Asset25=image
        saveImage("Project:SlimeInTankEmpty",Asset25)
        asset=26
        
    end
    
function gotImage26(image,status,header)
        
        Asset26=image
        saveImage("Project:Statistics",Asset26)
        asset=27
        
    end
    
function gotImage27(image,status,header)
        
        Asset27=image
        saveImage("Project:TimeMachine",Asset27)
        asset=28
        
    end
    
function gotImage28(image,status,header)
        
        Asset28=image
        saveImage("Project:Toaster",Asset28)
        asset=29
        
    end
    
function gotImage29(image,status,header)
        
        Asset29=image
        saveImage("Project:UnluckyDough",Asset29)
        saveLocalData("Download",1)
        asset=30
        
    end
        
end

I am guessing I am doing something wrong with the variable ‘asset’ since it only downloads the first image and then stops. Thanks in advance.

Here’s a small example that downloads one image after another and draws them as they download. Just change the urls list to change the images.

-- MultiDownload

-- execute f on next frame
local function nextFrame(f)
    tween.delay(0.001, f)
end

local function downloadNext(t, results)
    if #t > 0 then   
        http.request(t[1], function(img)
            table.insert(results, img)
            
            nextFrame(function()
                downloadNext(t, results)
            end)
        end)

        table.remove(t, 1)
    end
end

-- Use this function to perform your initial setup
function setup()
    local urls = {
        "http://codea.io/reference/images/ChapterIconLua.png",
        "http://codea.io/reference/images/ChapterIconNetwork.png",
        "http://codea.io/reference/images/ChapterIconVector.png",
        "http://codea.io/reference/images/ChapterIconGraphics.png",
        "http://codea.io/reference/images/ChapterIconTouch.png",
        "http://codea.io/reference/images/ChapterIconDisplay.png",
    }
    
    images = {}
    
    downloadNext(urls, images)
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(194, 215, 220, 255)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    spriteMode(CORNER)
    for _,v in pairs(images) do
        sprite(v, 0, 0)
        translate(0, v.height)
    end
end

This may be an easier way to do it

https://coolcodea.wordpress.com/2013/05/23/62-3d-downloading-images-on-demand/

more here

https://coolcodea.wordpress.com/2013/08/07/101-talking-to-the-internet/

Thanks both, this is a lot simpler than what I was trying to do!

@Simeon So How do I save the images using this method!

@JonoGaming00 well you could replace the images output table with an input table of save destinations, and instead of adding the result to the output table in downloadNext you just save the asset under the provided name and remove the name from the list.

@Simeon Thanks Again

@Ignatz, neat piece of code there. Useful explanation