Forum Discussion

scorpiontahir02's avatar
12 years ago

roUrlTransfer.AsyncGetToFile not working

Hi,

I am working on an application in which i need to fetch some images from internet. I want to do it asynchronusly as not all the images are used at the same time and i do not want my application to halt for some time. But the problem is roUrlTransfer.AsyncGetToFile is not creating file in tmp directory. I have add my async function in urlUtils.brs and here is the code:

It does not work when i do this:

function http_async_get_to_file(filename as String)
m.Http.EnableFreshConnection(true) 'Don't reuse existing connections
res = m.Http.AsyncGetToFile(filename)
end function


but it does work when i do this:

function http_async_get_to_file(filename as String)
m.Http.EnableFreshConnection(true) 'Don't reuse existing connections
res = m.Http.AsyncGetToFile(filename)
if (res)
event = wait(0, m.Http.GetPort())
if type(event) = "roUrlEvent"

else if event = invalid
m.Http.AsyncCancel()
return false
endif
endif

return true
end function


and i am calling this function like this:


for i = 1 to m.adImageCount - 1
asyncGetImage(m.content.adImageUrls.GetEntry(i))
end for
function asyncGetImage(url As string)
fileName = url
while fileName.Instr("/") <> -1
fileName = fileName.Mid(fileName.Instr("/") + 1)
end while
http = NewHttp(url)
print http.AsyncGetToFile("tmp:/" + fileName)
end function


So can you please guide why it is not working in completely async way (1st implementation)?

I am using the images after 20 seconds so this cannot be the case that images could not be downloaded as in 2nd implementation 1 image takes only 1 second or 2.

3 Replies

  • The problem is that your "http" variable is going out of scope as soon as your asyncGetImage function returns, so it's getting disposed before it has a chance to complete the transfer. You could try something like this, instead:

    imageTransfers = []
    for i = 1 to m.adImageCount - 1
    imageTransfers.Push(asyncGetImage(m.content.adImageUrls.GetEntry(i)))
    end for
    function asyncGetImage(url As string)
    fileName = url
    while fileName.Instr("/") <> -1
    fileName = fileName.Mid(fileName.Instr("/") + 1)
    end while
    http = NewHttp(url)
    print http.AsyncGetToFile("tmp:/" + fileName)
    return http
    end function

    That will keep the url transfers in scope via the imageTransfers array, which you can clean up after the images have been downloaded.
  • Thank you very much TheEndless! It solved the problem. I never thought it could be an issue regarding to scope of objects....
  • Is there a complete example of this because I'm having trouble getting the actual image so I can create a bitmap object from it.