download file

Hi!
I can’t get a download of the file by link in the emulator.
On the python program everything downloads:

import urllib.request
destination = 'testpict2.jpg'
url = 'https://upload.wikimedia.org/wikipedia/commons/5/5e/Messier83_-_Heic1403a.jpg'
urllib.request.urlretrieve(url, destination)

on Solar2d is downloaded only at the reference of the type
'https://i.postimg.cc/wtbm4xr9/2016-singapur-rochor-wi -tynia-sri-krishnan-21.jpg ’
by other links the error is not issued, but not downloaded.

here is the code

local function networkListener(event)

    if (event.isError) then

        print("Network error: ", event.response)

        local alert = native.showAlert("Network error:", event.response, { "Close app" },

            function() native.requestExit() end)

    elseif (event.phase == "began") then

        if (event.bytesEstimated <= 0) then

            print("Download starting, size unknown")

        else

            print("Download starting, estimated size: " .. event.bytesEstimated)

        end

    elseif (event.phase == "progress") then

        if (event.bytesEstimated <= 0) then

            print("Download progress: " .. event.bytesTransferred)

        else

            print("Download progress: " .. event.bytesTransferred .. " of estimated: " .. event.bytesEstimated)

        end

    elseif (event.phase == "ended") then

        print("Download complete, total bytes transferred: " .. event.bytesTransferred)

        local pict = display.newImageRect("pict.jpg", system.DocumentsDirectory, display.actualContentWidth,

            display.actualContentHeight)

        pict.anchorX = 0

        pict.anchorY = 0

        pict.x = 0 + display.screenOriginX

        pict.y = 0 + display.screenOriginY

    end

end

local params = {}

params.progress = "download"

params.response = {

    filename = "pict.jpg",

    baseDirectory = system.DocumentsDirectory

}

os.remove(system.pathForFile("pict.jpg", system.DocumentsDirectory))

--url = "https://upload.wikimedia.org/wikipedia/commons/5/5e/Messier83_-_Heic1403a.jpg?download"

--url = "https://upload.wikimedia.org/wikipedia/commons/9/92/Fortifications_Rhodes.jpg"

--url = "https://www.dropbox.com/scl/fi/1z45uibsllyp2614a2znz/p001.jpg?rlkey=6hpeqoqd4ptf44wumpqc5lwup&st=ps4yk3yj&dl=1"

url = "https://i.postimg.cc/wTBm4xR9/2016-Singapur-Rochor-wi-tynia-Sri-Krishnan-21.jpg"

network.download(

    url,

    "GET",

    networkListener,

    params,

    "pict.jpg",

    system.DocumentsDirectory

)

to console:
15: 12.176 Download Side: 1005
15: 26: 12.176 Download Progress: 1005 of Estimated: 1005
15: 12.176 Download Complete, Total Bytes Transferred: 1005
15: 12.176 Warning: D: \ Work \ TestDownLoad \ Main.lua: 24: 24: 24: 24: FA ILED To find IMAGE ’ PICT.JPG ’
15: 26: 12.176 error: runtime error

what am I doing wrong?

I’ve just tried those 4 urls, and all 4 downloaded successfully with no real changes made to the code.
The only change I made was to change “pict.jpg” to “pict1.jpg”, “pict2.jpg” etc so that I didn’t overwrite each image with the next one.

My guess would be a network issue. Either something on the network is blocking the request, or the request is timing out.

Of the 4 images you are trying to download in those URLs, the one that works for you also happens to be the smallest image (about 500kb). The others are 3.1mb, 5.1mb and 130mb so if you have a slow connection it could just be failing for that reason.

If you are not getting errors, I suggest checking the listeners event table fully to see if anything in there gives a clue:

function table.print(t)

    local print_r_cache = {}

    local function sub_print_r(t, indent)
            if (print_r_cache[tostring(t)]) then
                    print(indent .. "*" .. tostring(t))
                    --  str =  str .. indent.."*"..tostring(t)
            else
                    print_r_cache[tostring(t)] = true
                    if (type(t) == "table") then
                            for pos, val in pairs(t) do
                                    if type(pos) == "table" or type(pos) == "userdata" then

                                    else
                                            if (type(val) == "table") then
                                                    print(indent ..
                                                            "[" .. pos .. "] => " .. tostring(t) .. " {")
                                                    
                                                    sub_print_r(val,
                                                            indent .. string.rep(" ", string.len(pos) + 8))
                                                    print(indent ..
                                                            string.rep(" ", string.len(pos) + 6) .. "}")
                                                   
                                            elseif (type(val) == "string") then
                                                    print(indent .. "[" .. pos .. '] => "' ..
                                                            val .. '"')
                                            else
                                                    print(indent .. "[" ..
                                                            pos .. "] => " .. tostring(val))
                                            end
                                    end
                            end
                    else
                            print(indent .. tostring(t))
                    end
            end
    end

    if (type(t) == "table") then
            print(tostring(t) .. " {")
            sub_print_r(t, "  ")
            print("}")
    else
            sub_print_r(t, "  ")
    end

end

local function networkListener(event)
        print("networkListener:")
	table.print(event)
end

And the last thing I can think of is that you are successfully downloading the images, but they haven’t finished saving to disk when you attempt to draw them to the image rect. You could try adding a delay between the “ended” event and the draw call:

local function networkListener(event)    
    if (event.isError) then
       
    elseif (event.phase == "began") then

    elseif (event.phase == "progress") then

    elseif (event.phase == "ended") then

        print("Download complete, total bytes transferred: " .. event.bytesTransferred)

        local function drawTheImage()
        	local pict = display.newImageRect("pict.jpg", system.DocumentsDirectory, display.actualContentWidth, display.actualContentHeight)

        	pict.anchorX = 0

        	pict.anchorY = 0

        	pict.x = 0 + display.screenOriginX

        	pict.y = 0 + display.screenOriginY
        end

        timer.performWithDelay(100, drawTheImage)

    end
end

Thanks to everyone, but not one of the tips helped.

It seems that it does not work due to redirecting the request.

Everything successfully works at another hosting.